Click here to Skip to main content
15,887,929 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there is a scenario, where I need to copy folder files from local pc to my remote RDP server folder using c# console .is this the right way? what should i pass to the targetdirectory path?

What I have tried:

C#
class Program
{
    static void Main(string[] args)
    {
        string sourceDirectory = @"\\192.168.0.163\Y$\Logs";
        string targetDirectory = @"\\ ?\X$";

        Copy(sourceDirectory, targetDirectory);
    }



    public static void Copy(string sourceDirectory, string targetDirectory)
    {
        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

        CopyAll(diSource, diTarget);
    }

    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        Directory.CreateDirectory(target.FullName);

        // Copy each file into the new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            string  Filename= DateTime.Now.ToString("MM/dd/yyyy") + "_" +  fi.Name  ;
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, Filename);
            fi.CopyTo(Path.Combine(target.FullName, Filename), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }


}
Posted
Updated 23-Sep-19 6:52am
v2
Comments
ZurdoDev 23-Sep-19 10:04am    
I believe that will only work if you have a mapped drive or a share to the other computer.
Andy Lanng 23-Sep-19 12:35pm    
It would work if you are running as a user that has already logged on to that server, either via a mapped drive or any other "explorer" style method.
I'd tell you to use sFTP if the machine isn't local or use a mapped drive if it is.
You can run scripts to map a drive, then have your app run that script when the drive isn't mapped for the user it runs as

Does this[^] help?
 
Share this answer
 

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