eto*_*bot 5 c# windows-services
我希望在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);
}
}
Run Code Online (Sandbox Code Playgroud)
服务器共享将是:
string networkShare = @"\\ServerName\Share\";
Run Code Online (Sandbox Code Playgroud)
另请记住,服务正在执行的标识将影响服务是否能够保存到该位置。如果您使用域服务帐户来运行服务,请确保调整共享所在计算机上的目标共享文件夹上的 ACL 以允许写入