Windows service can't copy to file share

2020-06-19 11:09发布

问题:

I have a windows service set to copy files from a local directory to a samba share.

The service connects via a UNC path to the server (i.e. \remoteserver\shareddir).

I have logged in as the user under which the service is running, and was able to both copy files and create files on the samba share, using the UNC path.

However, when running the service I am getting io exceptions that suggest a login error. I have run process monitor on that box, watching the service and I can see it finding the UNC path but getting a login error when trying to write to the share.

This is using plain vanilla .net System.IO.File.Copy call.

I don't understand the errors, as when logged in under the same user I am able to copy and create files on the share.

Any ideas?

Edit: Exception text

Error in main processing loop (DoWork()): Logon failure: unknown user name or bad password.

System.Exception: Error on MyService.ProcessExportTransferfileDirectories and values: D:\Export, \RemoteServer\intray, D:\Export\Archive ---> System.IO.IOException: Logon failure: unknown user name or bad password.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.IO.File.Move(String sourceFileName, String destFileName)
at MyService.Consumer.BusinessLogic.WmsFacade.MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory logCategory, LogCounter logCounter) --- End of inner exception stack trace --- at MyService.Consumer.BusinessLogic.WmsFacade.MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory logCategory, LogCounter logCounter) at MyService.Consumer.BusinessLogic.WmsFacade.ProcessExportTransfer(IDictionary`2 fileDirectories) at MyService.Core.TimedWorkerProvider.DoWork()

回答1:

It ended up being a permissions issue - after running the service as a different user that was setup on the samba share it ran without a problem, copying files across.

Still don't understand why when using a different account I could copy/create files on the share using windows explorer but when the service was running under the same account it couldn't access the share at all :(



回答2:

I would recommend mapping a drive first and then doing the file copy to the mapped drive instead of the share.

You can map a drive using the dos net use command. For example:

System.Diagnostics.Process.Start("net", "use h: \remoteserver\shareddir /User:domian\me password")

This should resolve any login issues.



回答3:

A very helpful tool in your situation is process monitor. Just run it as administrator and add the following filter:

Process Name is "your_process.exe" AND Operation is "CreateFile"

You will see detailed information about the files your process is trying to use.



回答4:

As mentioned, when I log in with the same account that the service is running as, I have no problems copying to and creating files on the share. – Oded 1 min ago

Did you grant the user account the service is running as "Logon as Service" permissions in the local security policy??