使用 UNC 和模拟复制文件
我想使用 FileInfo 和 CopyTo 通过网络移动一些文件。我必须将文件移动到必须使用特定用户帐户访问的服务器上的共享位置。我该如何执行此操作 - 我是否必须冒充该用户然后进行复制?
我正在使用 .net 4,想知道完成模拟的最佳方法是什么。我读过有关使用 pInvoke 和使用 advapi32.dll 的信息,但我希望有人可以推荐一种更好的方法来执行此操作。
感谢您的任何想法。
编辑 感谢您的回复。这不是一个服务,它是一个控制台应用程序,但它将在多台机器上运行。使用映射比使用模拟有什么优势,反之亦然?推荐的方法是什么?
我还考虑过使用批处理文件来创建映射并进行复制,但我不确定完成起来有多容易,因为要复制的文件夹并不总是相同的 - 它总是在一个目录中,但子目录名称发生变化。
I want to use FileInfo and CopyTo to move some files across a network. I have to move the files to a share on a server that has to be accessed using a particular user account. How do I do this - do I have to impersonate that user and then do the copy?
I am using .net 4 and was wondering what the best way accomplish the impersonation is. I've read about using pInvoke and using advapi32.dll, but I was hoping someone could recommend a better way to do this.
Thanks for any thoughts.
EDIT
Thanks for the replies. This is not a service, it is a console app, but it will be run from multiple machines. Is there any advantage to using a mapping over using impersonation, or vice-versa? what is the recommended approach?
I have also considered using a batch file to create the mappings and do the copy, but I wasn't sure how easily it would be to accomplish because the folders to copy from will not always be the same - it will always be within one directory, but the subdirectory name changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要模拟,您所要做的就是使用您想要使用的凭据建立文件映射。您可以使用 net use 作为 shell out 命令或 WNetAddConnection2
You don't need impersonation all you have to do is to establish a file mapping using the credentials you want to use. You can accomplish this using either net use as a shell out command or WNetAddConnection2
如果您作为服务运行,则可能需要模拟。这不是完整的代码,而是其要点:
保存令牌,然后再保存
If you are running as a service, you may need to impersonate. This is not the complete code but the gist of it:
Save the token and then later