Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.40/5 (2 votes)
See more:
Hello Everyone

I want to code to transfer a file from one computer to another computer through network in c#

Can anyone help me

Thanks In Advance
Posted
Updated 10-Jan-19 22:03pm
v2

To copy file from one machine to another through LAN, you need some share folder on destination machine.
Then you can access it using following syntax

C#
System.IO.File.Copy("sourcePath", "\\machinename\share folder path");

//but if you want to paste it on specific drive then you can use following code
System.IO.File.Copy("sourcePath", "\\machinename\DriveLetter$\folder name");
//e.g. System.IO.File.Copy("D:\\test.txt", "\\machinename\E$\test\test.txt");
 
Share this answer
 
Comments
.net developer123456789 26-May-16 5:28am    
Thanks a lot..Now working
koolprasad2003 26-May-16 5:49am    
Please share you code, so that we can help you
.net developer123456789 26-May-16 5:54am    
The file '\\Test-PC\UploadTest\test.txt' already exists.
please any idea ?
koolprasad2003 26-May-16 6:08am    
Do you want to replace existing file ?
.net developer123456789 26-May-16 6:05am    
Code Here :

public static void UploadShareFile(string strfilename)
{
try
{
string fileName = Path.GetFileName(strfilename);
System.IO.File.Copy(strfilename, "\\\\Admin-PC\\Upload\\" + fileName);

}
catch (Exception ex)
{
WriteToErrorFile("UploadShareFile Method Error on: {0} " + ex.Message);
}
}
Basically, it's just a case of calling File.Move: File.Move Method (String, String) (System.IO)[^] and providing the source and destination paths.
The problems occur if the remote machine requires a login, in which case the simplest solution is to create a "virtual disk" on your PC which is connected to the folder on the remote PC (I do this to my NAS to make everything simpler, and connect "drive letters" to a number of different folders for various purposes). You can do this in Windows Explorer, via the "Map network drive" option of the network context menu.
If you don't, then you probably need to use impersonation:
C#
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity wid = new WindowsIdentity(username, password);
WindowsImpersonationContext context = wid.Impersonate();

File.Move(pathToSourceFile, "\\\\Server\\Folder");

context.Undo();
 
Share this answer
 
Comments
.net developer123456789 26-May-16 4:58am    
I got error something like this :- The name provided is not a properly formed account name.
OriginalGriff 26-May-16 5:05am    
Probably, your username is in the wrong format: it should be "user@domain"
Member 13968693 13-Sep-18 22:43pm    
In this line: WindowsImpersonationContext context = wid.Impersonate();
I got this error: System.Reflection.TargetException: Error in the application.
Member 15345338 9-Sep-21 5:20am    
in this line:WindowsIdentity wid = new WindowsIdentity(@"Administrator@MYLAB.LOCAL", "Password1");
i got a error:System.Security.SecurityException: 'The user name or password is incorrect.
' than i entered a destination machine user name and password have same error. i can't solve it

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