[英]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). 我想在Windows Server 2008服务器上运行Windows服务,该服务将监视本地服务器上的目录(即C:\\ Watch),并在该目录中创建新的pdf时,将文件复制到网络共享(例如//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. Windows服务已将其登录设置为本地用户帐户,该帐户可以访问// server / share并创建和删除文件而没有问题。
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". 如果sourceDir和destDir是本地文件夹(例如C:\\ Source和C:\\ Dest),但如果我将destDir更改为网络位置(例如// server / share /或///// server /),则可以使用以下命令/ share //我收到错误消息“ T文件名,目录名或卷标签语法不正确”。
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. 更新:我不再遇到上述错误,现在当我将sourceDir设置为C:\\ Watch并且将destDir设置为\\ server \\ share \\(服务器可以是Windows或Ubuntu Server时,我得到一个System.UnauthorizedAccess我假设这是来自目标服务器的错误。如何设置连接到目标服务器时使用的凭据。请记住,这些服务器不在域中,可以是Windows或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);
}
}
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
如果您使用域服务帐户来运行服务,请确保从共享所在的计算机上调整目标共享文件夹上的ACL,以允许写入
基于Oded的回答, 在我以本地用户身份运行该服务后,该用户也在远程Ubuntu服务器上进行了设置,所有工作都像一个魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.