Windows服务无法复制到文件共享。

时间:2023-01-17 15:31:26

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

我有一个windows服务集,用于将文件从本地目录复制到samba共享。

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

该服务通过UNC路径连接到服务器(即\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.

我作为运行服务的用户登录,并且能够使用UNC路径在samba共享上复制文件和创建文件。

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.

然而,在运行该服务时,我将获得提示登录错误的io异常。我在那个框上运行了process monitor,监视这个服务,我可以看到它找到UNC路径,但是当尝试写入共享时,会出现登录错误。

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

这是使用普通的。net System.IO.File。复制的电话。

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.

主处理循环错误(DoWork()):登录失败:未知用户名或错误密码。

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.

系统。例外:在MyService错误。processexporttransferfiledirectory和值:D:\Export, \RemoteServer\intray, D:\Export\Archive——> System.IO。IOException:登录失败:未知用户名或错误密码。

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()

在System.IO.__Error。WinIOError(Int32 errorCode, String maybeFullPath)位于System.IO.__Error.WinIOError()位于System.IO.File。在MyService.Consumer.BusinessLogic.WmsFacade移动(String sourceFileName, String filename)。MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory LogCategory, LogCounter LogCounter LogCounter)——内部异常堆栈跟踪的结尾——在MyService.Consumer.BusinessLogic.WmsFacade。MoveAndArchiveFile(String sourceFolder, String targetFolder, String archiveFolder, String rootFolder, LogCategory LogCategory, LogCounter LogCounter LogCounter)位于MyService.Consumer.BusinessLogic.WmsFacade。在MyService.Core.TimedWorkerProvider.DoWork ProcessExportTransfer(IDictionary 2 fileDirectories)()

4 个解决方案

#1


4  

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.

它最终导致了一个权限问题——在将服务作为一个不同的用户运行之后,这个用户是在samba共享上安装的,并且没有出现任何问题,而是将文件复制到整个samba上。

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 :(

我仍然不明白为什么在使用不同的帐户时,我可以使用windows资源管理器在共享上复制/创建文件,但是当服务在同一个帐户下运行时,它根本无法访问共享:

#2


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:

可以使用dos net use命令映射驱动器。例如:

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

This should resolve any login issues.

这将解决任何登录问题。

#3


1  

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


0  

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

如前所述,当我以服务运行的相同帐户登录时,复制和创建共享文件没有问题。- 1分钟前。

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

您是否在本地安全策略中将服务运行的用户帐户授予“作为服务登录”权限?

#1


4  

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.

它最终导致了一个权限问题——在将服务作为一个不同的用户运行之后,这个用户是在samba共享上安装的,并且没有出现任何问题,而是将文件复制到整个samba上。

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 :(

我仍然不明白为什么在使用不同的帐户时,我可以使用windows资源管理器在共享上复制/创建文件,但是当服务在同一个帐户下运行时,它根本无法访问共享:

#2


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:

可以使用dos net use命令映射驱动器。例如:

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

This should resolve any login issues.

这将解决任何登录问题。

#3


1  

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


0  

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

如前所述,当我以服务运行的相同帐户登录时,复制和创建共享文件没有问题。- 1分钟前。

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

您是否在本地安全策略中将服务运行的用户帐户授予“作为服务登录”权限?