C# 如何将文件从UNC共享复制到本地系统?
我被这个问题难住了 我有UNC共享,我知道帐户详细信息,可以完全访问,但不能访问我的本地系统。 我可以通过以下方式访问远程UNC:C# 如何将文件从UNC共享复制到本地系统?,c#,.net,unc,windows-identity,C#,.net,Unc,Windows Identity,我被这个问题难住了 我有UNC共享,我知道帐户详细信息,可以完全访问,但不能访问我的本地系统。 我可以通过以下方式访问远程UNC: var token = default(IntPtr); var context = default(WindowsImpersonationContext); LogonUser(_config.Username, _config.Domain, _config.Password, 2, 0, out token); context = WindowsIdenti
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);
但是,我无法在模拟过程中访问我的本地系统,因为帐户无权访问它
在这种情况下如何复制文件?我是否需要使用缓冲区之类的功能并打开/关闭模拟功能?您需要做的是读取所有字节,然后写入它们:
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);
}
但是,文件是巨大的~200Mb-~1Gb。。此外,这也是系统的一部分,它可以有几个“unc文件存储”。它将复制不同线程中的文件。。。我真的不想每个文件都读。。它会吃掉所有记忆我找到了解决办法。。。它的名字是WNetUseConnection@Oskar-在使用Windows Server 2003/IIS6.x时,这对我来说很有吸引力,但在使用Windows Server 2008/IIS 7.5时,只有当用户在我尝试连接的系统上拥有“本地”用户帐户时,它才会起作用(至少据我所知)。所以,是的,非常希望看到一个非管理员帐户也能解决这个问题。愚蠢的Windows安全性:-\