Click here to Skip to main content
15,891,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted
Updated 1-Jun-16 23:42pm
v3

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]
 
Share this answer
 
v3
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
C#
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
 
Share this answer
 
Comments
ranio 2-Jun-16 6:01am    
string username = @"Alexander.junior@chn.nestgroup.net";
string password = "lordsusan126*";

//\\ntp-228\Alex\SIB_TEST

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();
string updir = @"Z:\abcd.txt";
string destPath = @"D:\Alex_BackUp\Test\abcd.txt";

File.Copy(updir,destPath, true);
I am getting the access to the path denied option. Folder shared has admin privilege
koolprasad2003 2-Jun-16 6:23am    
first try to open a mapped drive ? does it prompt for authentication ? if yes the put user name and password and open a map folder then try your code.
Note : when you put user name and password in authentication, do not forget to check 'remember this password' checkbox
ranio 2-Jun-16 6:44am    
Did as mentioned above. still getting the access denied option. I can open the file. But while trying to copy the file access denied issue shown after impersonate
Prateek Dalbehera 2-Jun-16 8:07am    
May you dont have access to copy files.
In my config file, I access mapped drive by using the below path -
Z:\\work
I take this value from config file & convert it to a string & use.
koolprasad2003 2-Jun-16 8:20am    
access denied means you have limited rights. Do one thing
Open map drive and try to create a TXT file there, by right click and create new text file. Does it works ? if not the first give access permission to map drive then your code will work as expected

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900