如何通过C#连接到映射的网络驱动器? [英] How to connect to a mapped network drive via C#?

查看:527
本文介绍了如何通过C#连接到映射的网络驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从映射网络驱动器复制到计算机中的驱动器。映射网络驱动器是Z.在尝试从网络驱动器路径复制文件访问时,可以看到拒绝问题。

即使错误也可以使用凭证。



string username =alexander.junior;

string password =lordsusan126 *;

string updir = @\\Z: \ Alex \ SIB_TEST \abcd.txt;





AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsIdentity identity =新的WindowsIdentity(用户名,密码);

WindowsImpersonationContext context = identity.Impersonate();



File.Copy(updir,@D:\ Alex_BackUp\Test\abcd.txt,true);



我尝试过:



string username =alexander.junior;

string password =lordsusan126 *;

strin g updir = @\\Z:\ Alex \SIB_TEST \ abcd.txt;





AppDomain。 CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsIdentity identity = new WindowsIdentity(用户名,密码);

WindowsImpersonationContext context = identity.Impersonate();



File.Copy(updir,@D:\ Alex_BackUp\Test\abcd.txt,true);

I want to copy file from a Mapped Network Drive to a drive in machine. The Mapped Network drive is Z. On trying to copy file access from the network drive path denied issue is seen.
Also the credentials even if wrong is working.

string username = "alexander.junior";
string password = "lordsusan126*";
string updir = @"\\Z:\Alex\SIB_TEST\abcd.txt";


AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();

File.Copy(updir, @"D:\Alex_BackUp\Test\abcd.txt", true);

What I have tried:

string username = "alexander.junior";
string password = "lordsusan126*";
string updir = @"\\Z:\Alex\SIB_TEST\abcd.txt";


AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();

File.Copy(updir, @"D:\Alex_BackUp\Test\abcd.txt", true);

推荐答案

可能是你的用户名:它应该采用user @ domain格式。

另外,你的 updir 看起来不对:它不应该在Z:之前有\\

我讨厌降价!吞下其中一个反斜杠......:mad:[/ edit]
Probably, it's your username: it should be in the format "user@domain".
In addition, your updir looks wrong: it's shouldn't have "\\" in front of "Z:"
[edit]I hate markdown! Swallowing one of the backslashes... :mad: [/edit]


网络映射驱动器与您的硬盘驱动器相同(但在客户端计算机上进行逻辑映射),为什么要添加'\\ \\''''z:'? '\\'仅用于共享文件夹而不用于映射驱动器

使用下面的代码复制文件

Network mapped drive is same like your HDD drive (but logical mapped on your client machine), why you add '\\' to 'z:' ? '\\' is only used for shared folder not for mapped drive
use below code to copy your files
string updir = @"Z:\Alex\SIB_TEST\abcd.txt";
string destPath = @"D:\Alex_BackUp\Test\abcd.txt";
System.IO.Path.Copy(updir, destPath); 



希望有帮助


hope it helps


这篇关于如何通过C#连接到映射的网络驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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