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);
}
}
Once the app is installed, open File Explorer. The drive/network location must be online/connected/accessible. Right-click it and from the context menu, go to Path Copy. From the sub-menu, select the 'Short UNC path' or the 'Long UNC Path', whichever you need and the path will be copied to the clipboard.
To share a file or folder over a network in File Explorer, do the following: Right-click (or long-press) a file, and then select Show more options > Give access to > Specific people. Select a user on the network to share the file with, or select Everyone to give all network users access to the file.
To transfer files between 2 Windows servers, the traditional way is to use FTP desktop app as a middle-man. You need to download Filezilla or other FTP desktop tool, configure and use it to upload or download files between two remote servers.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With