我有一个网络共享驱动器(“\serveur\folder”),我想在其中复制文件。 我可以用特定用户(“用户”/“通行证”)在驱动器上写入。 如何使用 C# 访问具有写入权限的共享驱动器?
最佳答案
未经测试的代码,但它类似于:
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:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);
}
finally
{
context.Undo();
}
关于c# - 在网络共享驱动器上复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1432213/