如何将文件从UNC共享复制到本地系统

本文关键字:复制 系统 共享 UNC 文件 | 更新日期: 2023-09-27 18:00:39

我被这个问题卡住了。

我有UNC共享,我知道帐户详细信息,它可以完全访问,但不能访问我的本地系统。我可以使用访问远程UNC

var token = default(IntPtr);
var context = default(WindowsImpersonationContext);
LogonUser(_config.Username, _config.Domain, _config.Password, 2, 0, out token);
context = WindowsIdentity.Impersonate(token);
//TODO :: System.IO operations
File.Copy("remote-unc-path","local-path",true); // Exception : Access is denied.
context.Undo();
CloseHandle(token);

但是,我无法在模拟期间访问我的本地系统,因为帐户无法访问它。

在这种情况下如何复制文件?我需要使用缓冲区之类的东西并打开/关闭模拟吗?

如何将文件从UNC共享复制到本地系统

您要做的是读取所有字节,然后写入它们:

    var token = default(IntPtr);
    using (var context = default(WindowsImpersonationContext))
    {
       LogonUser(_config.Username, _config.Domain, _config.Password, 2, 0, out token);
       context = WindowsIdentity.Impersonate(token);
       var bytes = File.ReadAllBytes("remote-unc-path");
       context.Undo();
       CloseHandle(token);
       File.WriteAllBytes("local-path", bytes);
    }