Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Copy a file to Network Location from Windows Service?

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);
    }
}
like image 728
etoisarobot Avatar asked Mar 16 '11 18:03

etoisarobot


People also ask

How do I copy a file path to my network?

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.

How do I copy files over Windows network?

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.

How copy file from Windows Server to Windows Server?

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.


2 Answers

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

like image 193
Ta01 Avatar answered Sep 28 '22 01:09

Ta01


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.

like image 45
etoisarobot Avatar answered Sep 27 '22 23:09

etoisarobot