Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to copy folders from my system to another system using C#.

Another system is connected using LAN.

How to do this
Posted

Use the System.IO.File and System.IO.Directory classes.
E.g.

C#
string[] files = System.IO.Directory.GetFiles(sourcePath);

// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
    // Use static Path methods to extract only the file name from the path.
    fileName = System.IO.Path.GetFileName(s);
    destFile = System.IO.Path.Combine(targetPath, fileName);
    System.IO.File.Copy(s, destFile, true);
}
 
Share this answer
 
Comments
Herman<T>.Instance 3-Apr-14 4:50am    
what about credentials? If user is not allowed on second machine how would you copy then?
Abhinav S 3-Apr-14 6:39am    
Nope without credentials copy would not work.
C#
private void CopyFolder(string sourcePath, string DestPath, bool overWrite)
{
     DirectoryInfo dir = new DirectoryInfo(sourcePath);

     //iterate through all files
     foreach (FileInfo file in dir.GetFiles())
     {
           file.CopyTo(DestPath  + file.Name, overWrite);
     }
}


Note: Handle some possible errors by own like folder exists, path formats etc.
 
Share this answer
 
Comments
Herman<T>.Instance 3-Apr-14 4:51am    
what about credentials? If user is not allowed on second machine how would you copy then?
Sanjay K. Gupta 3-Apr-14 4:55am    
It will simply throw exception and that need to be handled.
Herman<T>.Instance 3-Apr-14 4:56am    
but no files copied
Sanjay K. Gupta 3-Apr-14 4:57am    
You can use below code for credential
using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
// code that executes under the new context.
File.Copy( x, y );
}
Source: http://stackoverflow.com/questions/4957522/file-copy-to-file-server-with-network-credential
You need to use an Impersonated User. With that impersonated user you acn copy to the second machine

C#
IntPtr admin_token = default(IntPtr);
                        IntPtr dupToken = default(IntPtr);

                        WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
                        WindowsIdentity wid_admin = null;
                        WindowsImpersonationContext wic = null;
                        string[] split = new string[1];
                        split[0] = "\\";
                        string[] temp = ServerName.Split(split, StringSplitOptions.RemoveEmptyEntries);
                        string domain = String.Empty;
                        string userName = ServerUserName;
                        string password = Password;
                        if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref admin_token) != 0)
                        {
                            if (DuplicateToken(admin_token, 2, out dupToken))
                            {
                                wid_admin = new WindowsIdentity(dupToken);
                                wic = wid_admin.Impersonate();
                                try
                                {
                                    // File.COPY thing
                                }
                                finally
                                {
                                    if (wic != null)
                                    {
                                        wic.Undo();
                                    }
                                    if (admin_token != default(IntPtr))
                                        CloseHandle(admin_token);

                                    if (dupToken != default(IntPtr))
                                        CloseHandle(dupToken);                             
                                }
                           }
 
Share this answer
 
Comments
KUMAR619 3-Apr-14 5:12am    
How to transfer folders from my system using C#
How to list out all the available IP address.
Then if I send it should ask for authentication information.

If authentication successful then it should be saved on the desktop of the new system.

Please help sir
KUMAR619 3-Apr-14 5:50am    
Please Explain sir
Herman<T>.Instance 4-Apr-14 2:58am    
You can search on CodeProject or Google how to iterate all IP addresses in your network in c#

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