Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
wanna to copy file from pc to server <username,password>,
when i wrote the follwing code and run it exception was thrown "The given path's format is not supported" ,any help
if (File.Exists(SrcFileName))
{
    DestFileName = "\\administrator:123456@10.10.2.24\\d$\\document\\documents\\photo$\\testcopy.bmp";
    DestFileName = "\\\\10.10.2.24/administrator:123456\\d$\\document\\documents\\photo$\\testcopy.bmp";
    if (File.Exists(DestFileName))
        MessageBox.Show("already exists in destination");
    else
        File.Copy(SrcFileName, DestFileName, true);
}
Posted
Updated 10-Jul-17 0:56am
v2

You cannot connect to a network drive by authenticating in this manner. Furthermore, there isn't a .NET class that accomodates this.

You have two options, invoke the Win32 API using interop, or spawn a "net use" command via the command interpreter.

This article[^] shows how to map a drive using interop. You don't need the complexity of this whole project, but scanning the source code will show you the essential steps.

Note, that you don't actually have to map a drive letter to do this, you can just use this method to connect to the remote share without mapping it. Once you have done so, you can invoke File.Copy() without trying to embed the credentials.
 
Share this answer
 
Comments
Espen Harlinn 22-May-11 12:17pm    
Fair reply, my 5
Kim Togo 22-May-11 12:39pm    
Good answer, my 5
Reem Moh 28-May-11 13:01pm    
Thank you
You have to impersonating a user, before File.Copy. Check the CP article A small C# Class for impersonating a User[^]

C#
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
  File.Copy(...) 
}
 
Share this answer
 
Comments
Espen Harlinn 22-May-11 12:17pm    
Nice link, my 5
Kim Togo 22-May-11 12:38pm    
Thanks
Reem Moh 28-May-11 12:59pm    
I tried this code ,it works
Thank you
Kim Togo 28-May-11 14:10pm    
Great! :-)
Reem Moh 2-Jun-11 3:15am    
'logon failure unknown username or bad password' error msg!!!
any help

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