c# - 在网络共享驱动器上复制文件

标签 c# authentication network-drive

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

相关文章:

api - 为用户处理 API 身份验证的最佳方式是什么(尤其是对于移动设备)

c# - 如何将任何类型的日期转换为 dd/mm/yyyy

C# for 循环,通过用户指定的数字给出 i/(i+1) 的总和

swift - 如何在 Swift 中使用自己的 anchor 证书实现 `URLSession:didReceiveChallenge:completionHandler:`?

linux - 远程 Linux 节点尝试访问共享网络驱动器的内容

python - Django MEDIA_ROOT 指向网络驱动器

php - 来自 Linux 环境下运行的 PHP 的 Windows 网络驱动器

c# - MVVM 中 View 的工厂

c# - DataBindingComplete 上 DataGridView 中的单元格格式

PHP:活跃用户