Hello, I'm trying to create a windows service that copies all the files in a directory on a network drive to a local folder. I am using UNC instead of a mapped name which is passed in from an .ini file. The serviceprocessinstaller1 is set to LocalSystem account. The following code always gives me an access denied error. I am reading that windows services are tricky to get to access network drives and I'm asking how to set this up with the installers. I tried to use the properties window of the service to use the name and logon of a user but that didnt work either. The code fails at the If sourceDir.exists part and logs the Log("Source directory is not available: " + SourceDir.FullName). My question is what is the correct way to set this up:
LocalSystem or one of the other 3?
Local System Account or This account from service properties window?
I don't want to hardcode the account name/password.
Does it matter that I'm using the .msi file generated by the Setup project of the Solution?
Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
' the source directory must exist, otherwise throw an exception
If SourceDir.Exists Then ' if destination SubDir's parent SubDir does not exist throw an exception
If Not DestDir.Parent.Exists Then
' Throw New DirectoryNotFoundException("Destination directory does not exist: " + DestDir.Parent.FullName)
Log("Destination directory does not exist: " + DestDir.Parent.FullName)
If Not DestDir.Exists Then
' copy all the files of the current directory
Dim ChildFile As FileInfo
For Each ChildFile In SourceDir.GetFiles()
If Overwrite Then
ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
' if Overwrite = false, copy the file only if it does not exist
' this is done to avoid an IOException if a file already exists
' this way the other files can be copied anyway...
If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
' copy all the sub-directories by recursively calling this same routine
Dim SubDir As DirectoryInfo
For Each SubDir In SourceDir.GetDirectories()
CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
Log("All files copied to destination: " + DestPath)
' Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
Log("Source directory is not available: " + SourceDir.FullName)
Catch ex As Exception