1. This code worked fine in the body of a sub main in a console app but needs adjustedment moving to a windows service app. I did not write the code ,but this project was dumped on me and I do not know VB.Net or C#. The problems
I see is since the windows service is using the filwatcherserver control and this sub is handled by : Private Sub FileSystemWatchService_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) the passing of sender and e requires me to not tell her in code
some of this info like before since its getting passed into the created event as args:
Dim _sDir As String = "C:\Temp"
Dim _dDir As New DirectoryInfo(_sDir)
Dim _fFileSystemInfo As FileSystemInfo
I think if you all can help me change these variables to simply use what the filewatcher is passing in as args I will be ok.
So my guess is only need to change this variable. but I am not sure.
Dim _fFileSystemInfo As FileSystemInfo >>>TO e.Name
Below I will explain what the projects purpose is overall incase anyone is curious for a better understanding,
but really if you are and intermediate or advanced developer in vb.net or C#, changing I think maybe 1, 2 or 3 variables Dim'd at the top and use for example "e.name" for the inplace of " _fFileSystemInfo " this seemingly huge learning curve of a project for me, to a few obvious changes for you! I hope.
Overall this code looks for new .pdfs files moved into a specific directory which kicks off the filewatcher's created event running in this window service to move the file to an appropriate folder and rename it and also based on certain conditions update an SQL table call 2 SP to do that. I just wanted to give you an overview of the overall purpose and scope of the project.
Private Sub FileSystemWatchService_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatchService.Created
'Event Created handles cut and pasted files , or files moved here
Dim _sDir As String = "C:\Temp"
Dim _dDir As New DirectoryInfo(_sDir)
Dim _fFileSystemInfo As FileSystemInfo
Dim _sFileNameParts() As String
Dim _cDelim() As Char = CType("_()", Char())
Dim _cDelim2() As Char = CType(" ", Char())
Dim connectionString As String = ConfigurationSettings.AppSettings("ConnString") 'Data Source=OurServer; Initial Catalog=ourdatabase; User ID=sa; Password=*****;
Dim _sSubjectFlags() As String = ConfigurationSettings.AppSettings("SubjectFlags").Split(",")
Dim _MyFolder As String = _sDir
Dim _RV As Int32 = -1
Dim _State As String = String.Empty
Dim connection As SqlConnection = New SqlConnection(connectionString)
connection.Open()
Try
'For Each _fFileSystemInfo In _dDir.GetFileSystemInfos(ConfigurationSettings.AppSettings("FileExt"))
_sFileNameParts = _fFileSystemInfo.Name.Split(_cDelim) '0 = CertificateNr, 1 = Report Type, 2 = National or Hawaii
Dim _filePrefix As String = String.Empty
Dim i As Integer = 0
For Each s As String In _sSubjectFlags
Dim sArray() As String = s.Split("=")
Dim _sSpecialChars As String = Regex.Replace(_fFileSystemInfo.Name.ToLower(), ConfigurationSettings.AppSettings("IgnoredFileCharacters"), String.Empty)
If (_sSpecialChars.Contains(sArray(0))) Then 'remove tolower
_filePrefix = sArray(1)
End If
i = i + 1
Next
If (_filePrefix.Length <> 0) Then
'Another command object to fetch state
Dim cmdState As SqlCommand = New SqlCommand("spGetLoc_State", connection)
cmdState.CommandType = CommandType.StoredProcedure
Dim _parmState As New SqlParameter("@CertificateNrClean", SqlDbType.VarChar, 25)
_parmState.Value = _sFileNameParts(0).Trim()
cmdState.Parameters.Add(_parmState)
Dim _rdr As SqlDataReader = cmdState.ExecuteReader()
If (_rdr.Read()) Then
_State = _rdr("Loc_State").ToString()
End If
_rdr.Close()
If (_State.Length > 0) Then
'Updating UW String if needed
Dim cmdUpdate As SqlCommand = New SqlCommand("sp_SiteInspections_Photo_UW_Update", connection)
cmdUpdate.CommandType = CommandType.StoredProcedure
Dim _parm As New SqlParameter("@CertificateNrClean", SqlDbType.VarChar, 25)
_parm.Value = _sFileNameParts(0).Trim()
cmdUpdate.Parameters.Add(_parm)
Dim _parmRV As New SqlParameter("@ReturnVal", SqlDbType.Int)
_parmRV.Direction = ParameterDirection.ReturnValue
cmdUpdate.Parameters.Add(_parmRV)
cmdUpdate.ExecuteNonQuery()
_RV = CInt(cmdUpdate.Parameters("@ReturnVal").Value)
End If
Else
_MyFolder = _sDir + ConfigurationSettings.AppSettings("No_StateFolder")
End If
'ReturnValue = 0 No Errors,1 = Photo Exisited, 2 = Nothing Selected,
Select Case _RV
Case 0
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderClientFiles") + _State + "\"
Case 1
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderPhotoExisted")
Case 2
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderNothingSelected")
End Select
If (_State.Length = 0 Or _filePrefix.Length = 0) Then
_State = String.Empty
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderProblematic")
End If
Dim _path As String
Dim _NewName As String
If (_State.Length = 0 Or _RV <> 0) Then
_path = _MyFolder
_NewName = _fFileSystemInfo.Name
Else
_path = _MyFolder + _sFileNameParts(0).Trim()
_NewName = "\" + _sFileNameParts(0).Trim() + "_" + _filePrefix + DateTime.Now.ToString(ConfigurationSettings.AppSettings("DateMask")) + _fFileSystemInfo.Extension
End If
Dim _junk As String = _NewName
Directory.CreateDirectory(_path)
If (System.IO.File.Exists(_path + _NewName)) Then
System.IO.File.Delete(_path + _NewName)
End If
System.IO.File.Move(_fFileSystemInfo.FullName, _path + _NewName)
'Next
connection.Close()
2. what will override? If I am using app.conf file to put values in keys for example file filter "*.pdf", directory to watch "c:\temp, and so on , will this override the file watchers arguments like Sender.Filter = "*.pdf"?
3.when using ionSettings.AppSettings("No_StateFolder")in place of the
I see is since the windows service is using the filwatcherserver control and this sub is handled by : Private Sub FileSystemWatchService_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) the passing of sender and e requires me to not tell her in code
some of this info like before since its getting passed into the created event as args:
Dim _sDir As String = "C:\Temp"
Dim _dDir As New DirectoryInfo(_sDir)
Dim _fFileSystemInfo As FileSystemInfo
I think if you all can help me change these variables to simply use what the filewatcher is passing in as args I will be ok.
So my guess is only need to change this variable. but I am not sure.
Dim _fFileSystemInfo As FileSystemInfo >>>TO e.Name
Below I will explain what the projects purpose is overall incase anyone is curious for a better understanding,
but really if you are and intermediate or advanced developer in vb.net or C#, changing I think maybe 1, 2 or 3 variables Dim'd at the top and use for example "e.name" for the inplace of " _fFileSystemInfo " this seemingly huge learning curve of a project for me, to a few obvious changes for you! I hope.
Overall this code looks for new .pdfs files moved into a specific directory which kicks off the filewatcher's created event running in this window service to move the file to an appropriate folder and rename it and also based on certain conditions update an SQL table call 2 SP to do that. I just wanted to give you an overview of the overall purpose and scope of the project.
Private Sub FileSystemWatchService_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatchService.Created
'Event Created handles cut and pasted files , or files moved here
Dim _sDir As String = "C:\Temp"
Dim _dDir As New DirectoryInfo(_sDir)
Dim _fFileSystemInfo As FileSystemInfo
Dim _sFileNameParts() As String
Dim _cDelim() As Char = CType("_()", Char())
Dim _cDelim2() As Char = CType(" ", Char())
Dim connectionString As String = ConfigurationSettings.AppSettings("ConnString") 'Data Source=OurServer; Initial Catalog=ourdatabase; User ID=sa; Password=*****;
Dim _sSubjectFlags() As String = ConfigurationSettings.AppSettings("SubjectFlags").Split(",")
Dim _MyFolder As String = _sDir
Dim _RV As Int32 = -1
Dim _State As String = String.Empty
Dim connection As SqlConnection = New SqlConnection(connectionString)
connection.Open()
Try
'For Each _fFileSystemInfo In _dDir.GetFileSystemInfos(ConfigurationSettings.AppSettings("FileExt"))
_sFileNameParts = _fFileSystemInfo.Name.Split(_cDelim) '0 = CertificateNr, 1 = Report Type, 2 = National or Hawaii
Dim _filePrefix As String = String.Empty
Dim i As Integer = 0
For Each s As String In _sSubjectFlags
Dim sArray() As String = s.Split("=")
Dim _sSpecialChars As String = Regex.Replace(_fFileSystemInfo.Name.ToLower(), ConfigurationSettings.AppSettings("IgnoredFileCharacters"), String.Empty)
If (_sSpecialChars.Contains(sArray(0))) Then 'remove tolower
_filePrefix = sArray(1)
End If
i = i + 1
Next
If (_filePrefix.Length <> 0) Then
'Another command object to fetch state
Dim cmdState As SqlCommand = New SqlCommand("spGetLoc_State", connection)
cmdState.CommandType = CommandType.StoredProcedure
Dim _parmState As New SqlParameter("@CertificateNrClean", SqlDbType.VarChar, 25)
_parmState.Value = _sFileNameParts(0).Trim()
cmdState.Parameters.Add(_parmState)
Dim _rdr As SqlDataReader = cmdState.ExecuteReader()
If (_rdr.Read()) Then
_State = _rdr("Loc_State").ToString()
End If
_rdr.Close()
If (_State.Length > 0) Then
'Updating UW String if needed
Dim cmdUpdate As SqlCommand = New SqlCommand("sp_SiteInspections_Photo_UW_Update", connection)
cmdUpdate.CommandType = CommandType.StoredProcedure
Dim _parm As New SqlParameter("@CertificateNrClean", SqlDbType.VarChar, 25)
_parm.Value = _sFileNameParts(0).Trim()
cmdUpdate.Parameters.Add(_parm)
Dim _parmRV As New SqlParameter("@ReturnVal", SqlDbType.Int)
_parmRV.Direction = ParameterDirection.ReturnValue
cmdUpdate.Parameters.Add(_parmRV)
cmdUpdate.ExecuteNonQuery()
_RV = CInt(cmdUpdate.Parameters("@ReturnVal").Value)
End If
Else
_MyFolder = _sDir + ConfigurationSettings.AppSettings("No_StateFolder")
End If
'ReturnValue = 0 No Errors,1 = Photo Exisited, 2 = Nothing Selected,
Select Case _RV
Case 0
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderClientFiles") + _State + "\"
Case 1
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderPhotoExisted")
Case 2
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderNothingSelected")
End Select
If (_State.Length = 0 Or _filePrefix.Length = 0) Then
_State = String.Empty
_MyFolder = _sDir + ConfigurationSettings.AppSettings("FolderProblematic")
End If
Dim _path As String
Dim _NewName As String
If (_State.Length = 0 Or _RV <> 0) Then
_path = _MyFolder
_NewName = _fFileSystemInfo.Name
Else
_path = _MyFolder + _sFileNameParts(0).Trim()
_NewName = "\" + _sFileNameParts(0).Trim() + "_" + _filePrefix + DateTime.Now.ToString(ConfigurationSettings.AppSettings("DateMask")) + _fFileSystemInfo.Extension
End If
Dim _junk As String = _NewName
Directory.CreateDirectory(_path)
If (System.IO.File.Exists(_path + _NewName)) Then
System.IO.File.Delete(_path + _NewName)
End If
System.IO.File.Move(_fFileSystemInfo.FullName, _path + _NewName)
'Next
connection.Close()
2. what will override? If I am using app.conf file to put values in keys for example file filter "*.pdf", directory to watch "c:\temp, and so on , will this override the file watchers arguments like Sender.Filter = "*.pdf"?
3.when using ionSettings.AppSettings("No_StateFolder")in place of the