开发者

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. I can get access to remote UNC with :

var token = default(IntPtr);
var context = default(WindowsImpersonationContext);
开发者_如何学CLogonUser(_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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜