使用 UNC 和模拟复制文件

发布于 2024-09-28 23:20:43 字数 408 浏览 6 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流云如水 2024-10-05 23:20:43

您不需要模拟,您所要做的就是使用您想要使用的凭据建立文件映射。您可以使用 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

一抹苦笑 2024-10-05 23:20:43

如果您作为服务运行,则可能需要模拟。这不是完整的代码,而是其要点:

     [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    private static extern unsafe int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref string lpBuffer, int nSize, IntPtr* arguments);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool CloseHandle(IntPtr handle);


IntPtr token = IntPtr.Zero;

            bool isSuccess = LogonUser(username, domain, password, impersonationType, Logon32ProviderDefault, ref token);
            if (!isSuccess)
            {
                RaiseLastError();
            }

            WindowsIdentity newIdentity = new WindowsIdentity(token);
            WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();

保存令牌,然后再保存

CloseHandle(token);

If you are running as a service, you may need to impersonate. This is not the complete code but the gist of it:

     [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    private static extern unsafe int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref string lpBuffer, int nSize, IntPtr* arguments);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool CloseHandle(IntPtr handle);


IntPtr token = IntPtr.Zero;

            bool isSuccess = LogonUser(username, domain, password, impersonationType, Logon32ProviderDefault, ref token);
            if (!isSuccess)
            {
                RaiseLastError();
            }

            WindowsIdentity newIdentity = new WindowsIdentity(token);
            WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();

Save the token and then later

CloseHandle(token);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文