我有一个网络共享驱动器(“ \ 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(); }