0

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));
user584018
  • 6,270
  • 11
  • 40
  • 78

1 Answers1

1

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

Thomas Schmidt
  • 354
  • 4
  • 10
  • I did mounting with my admin account and service runs under local system account. You mean both are different, hence don't know each other? – user584018 Oct 10 '18 at 06:48
  • Yes, they do not know about the mounts between accounts. There are some extreme hacks, login scripts or group policy stuff that might work in some cases, but it would just be much simpler to simply use the unc path instead directly, no need to change any code most likely. – Thomas Schmidt Oct 10 '18 at 07:38
  • Ok, I did mounting with account `A` and with same account `A` I'm running my window service as well. Even this is not working, any reason? – user584018 Oct 10 '18 at 07:56
  • 1
    because you mount in the current user session, the service runs under account A as well but need to interactively logon, creating a new session. There are some discussion here about this: https://stackoverflow.com/questions/182750/map-a-network-drive-to-be-used-by-a-service but again it will save you a lot of trouble in the long run if you just switch to a unc share instead of trying to get a drive letter to work in your service – Thomas Schmidt Oct 10 '18 at 08:04
  • Thanks a lot Thomas – user584018 Oct 10 '18 at 08:19