I’ve placed some logic (in progress) in a Windows Application Form because I’m new to .NET and I knew I could test the logic there. My goal is to place the same logic in an independent Class to be called from Windows Application.
Later, when I’m confident the logic in the Class is correct – I want to call the same Class from a Windows Service.
I’m totally new to VB.NET, and I stumbled on the syntax for creating the Class, even after reading through tutorials, MSDN, etc. I posted the dumbest question earlier, and there were two responses right away which got me past the issue. (Thanks again.)
I’ve hit another obstacle, and the references and examples I’m using don’t really address what I’m trying to accomplish.
Okay…
This is the code I have within my Windows Application Form1. I moved the logic into a Class and built the .DLL. Now I’d like to call the class from the Windows Application – from within the Sub myWatcher_Changed event where it was originally written – see below. (I’m using the FileSystemWatcher to watch a directory. Never mind the logic at this point. There are probably one hundred better ways to write it, but for now it seems to work -- famous last words, I know.)
My question is (finally) how do I execute the code which is now in a Class? I know this is a very long post -- thank you for taking the time to read it.
Later, when I’m confident the logic in the Class is correct – I want to call the same Class from a Windows Service.
I’m totally new to VB.NET, and I stumbled on the syntax for creating the Class, even after reading through tutorials, MSDN, etc. I posted the dumbest question earlier, and there were two responses right away which got me past the issue. (Thanks again.)
I’ve hit another obstacle, and the references and examples I’m using don’t really address what I’m trying to accomplish.
Okay…
This is the code I have within my Windows Application Form1. I moved the logic into a Class and built the .DLL. Now I’d like to call the class from the Windows Application – from within the Sub myWatcher_Changed event where it was originally written – see below. (I’m using the FileSystemWatcher to watch a directory. Never mind the logic at this point. There are probably one hundred better ways to write it, but for now it seems to work -- famous last words, I know.)
My question is (finally) how do I execute the code which is now in a Class? I know this is a very long post -- thank you for taking the time to read it.
Code:
Private Sub myWatcher_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles myWatcher.Changed
'ALL OF THE CODE BELOW IS MOVED INTO THE CLASS, AND
'I WANT TO CALL IT FROM HERE,
'WITHIN THE myWatcher_Changed EVENT.
'MY QUESTION IS HOW DO I EXECUTE THE FOLLOWING
'CODE WHICH IS NOW IN A CLASS.
Dim strFooFooFileName, strFileSequence As String
Dim strFileDate, strYear, strMonth, strDay As String
Dim strDate As String = Mid(CStr(Now), 1, 10)
Dim strFileTime, strHour, strMinute, strSecond, strAMorPM As String
Dim strTime As String = CStr(TimeOfDay)
Dim shtPosition As Short
strFileDate = strDate
'Get the month
shtPosition = InStr(strDate, "/") ' Returns 2 for a 1 digit month, 3 for a two digit month
If shtPosition = 2 Then '1 digit month
strMonth = "0" & Mid(strDate, 1, 1)
strDate = Mid(strDate, 3, 10)
ElseIf shtPosition = 3 Then '2 digit month
strMonth = Mid(strDate, 1, 2)
strDate = Mid(strDate, 4, 10)
End If
'Get the day
shtPosition = InStr(strDate, "/") ' Returns 2 for a 1 digit day, 3 for a two digit day
If shtPosition = 2 Then '1 digit day
strDay = "0" & Mid(strDate, 1, 1)
strDate = Mid(strDate, 3, 10)
ElseIf shtPosition = 3 Then '2 digit day
strDay = Mid(strDate, 1, 2)
strDate = Mid(strDate, 6, 10) 'two digit year
End If
'Get the year
strYear = Trim(strDate)
'Concatenate year, month, day to use as part of the file name.
strFileDate = Trim(strYear) & Trim(strMonth) & Trim(strDay)
'Get the hour
shtPosition = InStr(strTime, ":") ' Returns 2 for a 1 digit hour, 3 for a two digit hour
If shtPosition = 2 Then '1 digit month
strHour = "0" & Mid(strTime, 1, 1)
strTime = Mid(strTime, 3, 10)
ElseIf shtPosition = 3 Then '2 digit month
strHour = Mid(strTime, 1, 2)
strTime = Mid(strTime, 4, 10)
End If
'Get the minute
strMinute = Mid(strTime, 1, 2)
'Get the second
strSecond = Mid(strTime, 4, 2)
'Get AM or PM
strAMorPM = Microsoft.VisualBasic.Right(Trim(strTime), 2)
'Concatenate hour and minute to use as part of the file name.
strFileTime = Trim(strHour) & Trim(strMinute) & Trim(strSecond)
'When the file drops into the Watched Directory, an
'event is raised here.
'Get the FileName, make it upper case
strFooFooFileName = e.Name.ToUpper
If strFooFooFileName.StartsWith("FOOISSUE") Then
strFileSequence = Mid(strFooFooFileName, 8, 4)
Try
'Create an instance of StreamReader to
'read from a file.
Dim sr As StreamReader = New StreamReader("C:\BLURP\ISSUES\UGH\" & strFooFooFileName)
Dim sw As StreamWriter = New StreamWriter("C:\BLURP\ISSUES\i" & strFileDate & "_" & strFileTime & strAMorPM & "_" & strFileSequence & ".dat")
Dim line As String
Dim column1 As String
Dim column2 As String
Dim column3 As String
Dim column4 As String
Dim column5 As String
Dim column6 As String
Dim column7 As String
Dim column8 As String
Dim column9 As String
'Read and display the lines from the file until the
'end of the file is reached.
Do
'get line from FooFoo file
line = sr.ReadLine()
If line <> "" Then
column1 = "62"
column2 = Space(20)
column3 = Mid(line, 20, 10)
column4 = Mid(line, 33, 10)
column5 = Mid(line, 46, 10)
column6 = Mid(line, 58, 6)
column7 = Mid(line, 64, 22)
column8 = Space(4)
If line.StartsWith("CN ") Then
column9 = "V"
Else
column9 = Space(1)
End If
sw.WriteLine(column1 & column2 & column3 & column4 & column5 & column6 & column7 & column8 & column9)
End If
Loop Until line Is Nothing
sr.Close()
sw.Close()
'Move the file from the UGH directory to the UGH\ARCHIVE directory.
File.Move("C:\BLURP\ISSUES\UGH\" & strFooFooFileName, "C:\BLURP\ISSUES\UGH\ARCHIVE\" & strFileDate & "_" & strFileTime & strAMorPM & "_" & strFooFooFileName)
Catch ex As Exception
'Let the user know what went wrong.
Console.WriteLine("The file could not be read.")
End Try
End If
'I MOVED ALL OF THE ABOVE CODE INTO THE CLASS
End Sub