0

im using FileSystemWatcher object to retreive files from a folder.

The code works good for local machine drives/folders but throws exception when i use a NETWORK Path.

For some reason the code do not throw an exception if i start the service in debug mode. If i install it via installutil then this exception is thrown again. Maybe is there some permission issues but i don't know what...

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)
Benjamin Martin
  • 576
  • 1
  • 8
  • 27
  • according to the exception message, the path doesn't exist.. Make sure you're using the correct path. – Elmer Dantas May 15 '17 at 09:50
  • Hi, the path exists. Its a shared drive. The program can access the path when i start my visual studio in debug mode. After installation of the service as a windows service then the exception occurred. – Benjamin Martin May 15 '17 at 09:52
  • 1
    Check [here](http://stackoverflow.com/questions/3622089/windows-service-cant-access-network-share) and [here](http://stackoverflow.com/questions/16946312/how-come-my-windows-service-cannot-access-a-folder-with-files-in-them) these will probably helps you. – Elmer Dantas May 15 '17 at 10:03
  • 1
    What is the network path look like ? can you try using **\\?\ or @\\ or $** – Vinod Srivastav May 15 '17 at 10:24
  • @Vinod: Yes i can use the UNC, but only if i change the log on from local system account to my own account. But the usage of mapped drive "T:\" in this case is still blocked. – Benjamin Martin May 15 '17 at 11:51
  • So i suppose you need to run this exe in your own context, just to confirm: make a shortcut on the desktop --> press Shift & rightClick -->select **Run as different user** – Vinod Srivastav May 16 '17 at 02:38

2 Answers2

2

Drive letter mappings are a per-interactive login setting. The exception is telling you the truth, T:\INFORMATION TECHNOLOGY\bu indeed does not exist because for sessions other than your own session (for example the session 0 that the service runs under) the drive T:\ does not get mapped anywhere. The reason it works when you debug is because when you debug you are likely running the service inside your own session instead of inside session 0.

A similar problem happens when you try to access a mapped drive from a program launched from a UAC prompt because the UAC user is considered a "different user".

Possible further reading for potential workarounds "Map a network drive to be used by a service"

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

I think the shared drive is not accessible to the Local System Account, and you will have to configure the Windows Service to run as a specific user


picture

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40