Click here to Skip to main content
14,982,479 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");
   
Comments
.net developer123456789 26-May-16 5:28am
   
Thanks a lot..Now working
koolprasadd 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 ?
koolprasadd 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);
}
}
koolprasadd 26-May-16 6:08am
   
Do you want to replace existing file ?
.net developer123456789 26-May-16 6:42am
   
Yes,please help me
koolprasadd 26-May-16 7:02am
   
Just pass 'true' to copy function, it will helps to overwrite exiting file. see below
// Copy one file to a location where there is a file already exist
File.Copy("file-a.txt", "file-b.txt", true); // overwrite = true
.net developer123456789 26-May-16 7:32am
   
Thanks...
koolprasadd 26-May-16 7:33am
   
If it resolve your issue.. then accept this as solution
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();
   
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.

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