复制网络共享驱动器上的文件 [英] Copy file on a network shared drive

查看:27
本文介绍了复制网络共享驱动器上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络共享驱动器(serveurfolder"),我想在上面复制文件.我可以使用特定用户(用户"/通行证")在驱动器上写入.如何使用 C# 访问具有写权限的共享驱动器?

I have a network shared drive ("serveurfolder") on which I would like to copy file. I can write on the drive with a specific user ("user"/"pass"). How can I access the shared drived with write privilege using C#?

推荐答案

未经测试的代码,但将类似于:

Untested code, but it will be similiar to:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

// http://pinvoke.net/default.aspx/advapi32/LogonUser.html    
IntPtr token;
LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);

WindowsIdentity identity = new WindowsIdentity(token);

WindowsImpersonationContext context = identity.Impersonate();

try
{
    File.Copy(@"c:	empMyFile.txt", @"\serverfolderMyfile.txt", true);
}
finally
{
    context.Undo();
}

这篇关于复制网络共享驱动器上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆