Search code examples
c#servicewindow

Could not find a part of the path error when running windows service after installation


I have a machine 1 where I have a share folder,

`\\Machine1HostName\Share`

and inside this we have a folder Files.

Now I mounted above folder in machine 2 with below path,

Z:\Files

I have a window servive running in machine 2 where I'm trying to get files from path Z:\Files.

var sourceFiles = Directory.GetFiles(@"Z:\Files").Select(f => new FileInfo(f));

When I'm running window servive as debug mode as a console app, then there is no error, but when installing this and trying to run with local system account then I'm getting below error,

Exception - Could not find a part of the path 'Z:\Files'.

Here is full stack trace,

Exception -    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at ConsoleApp6.ConverterService.Start() in C:\Users\source\repos\ConsoleApp6\ConsoleApp6\ConverterService.cs:line 19

Note - If I'm changing path from "Z:\Files" with "\\Machine1HostName\Share\\Files", then there is NO error if running service under local system account.

var sourceFiles = Directory.GetFiles(@"\\Machine1HostName\Share\\Files").Select(f => new FileInfo(f));

Solution

  • If you logged in and mounted the share with your account then most like you use another account for the service and it will not know about this mounted share. Mounting shares like this is not machine specific, they are user specific.

    you should not need to mount anything but instead just access the shares directly with the unc path instead, like: \\somemachine\somefolder