【发布时间】:2011-03-16 18:05:41
【问题描述】:
我希望在 Windows Server 2008 服务器上运行 Windows 服务,该服务将监视本地服务器上的目录(即 C:\Watch),当在该目录中创建新的 pdf 时,将文件复制到网络共享(即。//192.168.1.2/Share)。
这两个服务器都不是域的成员。
Windows 服务已将其登录设置为本地用户帐户,该用户帐户可以访问 //server/share 并毫无问题地创建和删除文件。
如果 sourceDir 和 destDir 是本地文件夹,如 C:\Source 和 C:\Dest,我有以下工作正常,但如果我将 destDir 更改为网络位置,如 //server/share/ 或 /// /server//share// 我收到错误“T文件名、目录名或卷标语法不正确”。
更新: 我不再收到上面的错误,现在当我将 sourceDir 设置为 C:\Watch 并将 destDir 设置为 \server\share\ (服务器可以是 Windows 或 Ubuntu 服务器时,我收到 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);
}
}
【问题讨论】:
标签: c# windows-services