Reputation: 7794
I want to have a Windows Service running on a Windows Server 2008 server that will monitor a directory on the local server (ie. C:\Watch) and when a new pdf is created in that directory, copy the file over to network share (ie. //192.168.1.2/Share).
Neither of the server's are members of a domain.
The Windows Service has it's log on as set to a local user account who can access the //server/share and create and delete files no prob.
I have the following which works fine if the sourceDir and the destDir are local folders like C:\Source and C:\Dest but if I change the destDir to a network location like //server/share/ or ////server//share// I get the error "TThe filename, directory name, or volume label syntax is incorrect".
Update: I am no longer getting the error above and now when I have the sourceDir set to C:\Watch and the destDir set to \server\share\ (where the server can be a Windows or Ubuntu Server I get a System.UnauthorizedAccess error which I am assuming is coming from destination server. How can I set the credentials to use when connecting to the destination Server. Remember the the servers are not in a domain and can be windows or Ubuntu.
public partial class Service1 : ServiceBase
{
private FileSystemWatcher watcher;
private string sourceFolder;
private string destFolder;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.sourceFolder = Properties.Settings.Default.sourceDir;
this.destFolder = Properties.Settings.Default.destDir;
watcher = new FileSystemWatcher();
watcher.Path = this.sourceFolder;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.pdf";
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
}
protected override void OnStop()
{
}
private void watcher_Created(object source, FileSystemEventArgs e)
{
FileInfo fInfo = new FileInfo(e.FullPath);
while (IsFileLocked(fInfo))
{
Thread.Sleep(500);
}
System.IO.File.Copy(e.FullPath, this.destFolder + e.Name);
System.IO.File.Delete(e.FullPath);
}
}
Upvotes: 6
Views: 10130
Reputation: 7794
Based on Oded's answer here Once I ran the service as a local user that was also set up on the remote Ubuntu server everything worked like a charm.
Upvotes: 1
Reputation: 31610
The server share would be:
string networkShare = @"\\ServerName\Share\";
Also keep in mind, the identity the service is executing will impact whether the service will be able to save to that location. If you are using a domain service account for the service to run as, make sure you adjust the ACL's on the destination share folder from the machine that the share is on to allow writes
Upvotes: 5