[英]How to copy file from UNC-share to local system?
I'm stuck with this question.我被这个问题困住了。
I have UNC share, I know account details, which has fullaccess, but it doesn't have access to my local system.我有 UNC 共享,我知道帐户详细信息,它具有完全访问权限,但它无权访问我的本地系统。 I can get access to remote UNC with:我可以通过以下方式访问远程 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);
But, I can't access my local system during Impersonation, because account doesn't have access to it.但是,在模拟期间我无法访问我的本地系统,因为帐户无权访问它。
How to copy file in this situation?在这种情况下如何复制文件? Do i need to use something like buffer and turn on/off Impersonation?我是否需要使用缓冲区之类的东西并打开/关闭模拟?
What you have to do is to read all the bytes and then write them:您要做的是读取所有字节,然后将它们写入:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.