FileSystemWatcher.Path 网络共享驱动器例外

FileSystemWatcher.Path Exception for Network Shared Drive

我正在使用 FileSystemWatcher 对象从文件夹中检索文件。

该代码适用于本地机器 drives/folders 但是当我使用 NETWORK 路径时抛出异常。

出于某种原因,如果我在调试模式下启动服务,代码不会抛出异常。如果我通过 installutil 安装它,则会再次抛出此异常。 也许有一些权限问题,但我不知道是什么...

public LevelOneValidatedWatcher(string strLevelOneFolder)
{
    FileSystemWatcher objFSW = new FileSystemWatcher();
    objFSW.Path = strLevelOneFolder; /// <<<<<<<<<<<<    EXCEPTION THROWN HERE
    objFSW.Filter = "*.zip";
    objFSW.Created += new FileSystemEventHandler(myWatcher_Created);
    objFSW.Renamed += new RenamedEventHandler(mydWatcher_Renamed);
    objFSW.EnableRaisingEvents = true;
}

System.ArgumentException was unhandled
Message=The directory name T:\INFORMATION TECHNOLOGY\bu is invalid.
Source=System
StackTrace:
at System.IO.FileSystemWatcher.set_Path(String value)
at MA.BatchTool.ServerLibrary.LevelOneValidatedWatcher..ctor(String strLevelOneFolder) in D:\Dev\Projects\MailAssurety1.1\Development\SourceCode\MA_1.1\WebServerSide\WebServerLibraries\LevelOneValidatedWatacher.cs:line 23
at MA.BatchTool.ServerLibrary.ServerJobProcessor.StartFileWatcher(Object strPath) in D:\Dev\Projects\MailAssurety1.1\Development\SourceCode\MA_1.1\WebServerSide\WebServerLibraries\ServerJobProcessor.cs:line 32
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)

我认为 Local System Account 无法访问共享驱动器,您必须将 Windows 服务配置为 运行 作为特定用户


驱动器盘符映射是每次交互登录设置。异常告诉你真相,T:\INFORMATION TECHNOLOGY\bu 确实不存在,因为对于你自己的会话以外的会话(例如服务运行的会话 0),驱动器 T:\ 不会映射到任何地方。它在您调试时起作用的原因是因为当您调试时,您可能 运行 您自己的会话中的服务而不是会话 0 中的服务。

当您尝试从从 UAC 提示启动的程序访问映射驱动器时,会发生类似的问题,因为 UAC 用户被认为是 "different user"。

可能进一步阅读潜在的解决方法“Map a network drive to be used by a service