Copy files or folders

This first example is just to copy  a single file

try
            {
                System.IO.File.Copy(@"C:\Users\pedro.falcao\Desktop\Falcao_ficheiros\Curioso.txt", @"\\florenciosousa\SHARE\Curioso.txt" /*@"\\florenciosousa\Users\florencio.sousa\Desktop\Curioso.txt"*/, true);
                Console.Write("copy sucessful");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.Write("path not found");
                Console.ReadLine();
            }

This second example is to copy all the file by type from a folder to other

 private void Copy2()
        {
            string sourceDir = @"C:\Shaymurtagh_Programs\TEKLA\2.firm_shay_murtagh\Drawing Details\TANKS";
            string backupDir = @"D:\pedro\Work";

            try
            {
                string[] dwgList = System.IO.Directory.GetFiles(sourceDir, "*.dwg");

                // Copy text files.
                foreach (string f in dwgList)
                {

                    // Remove path from the file name.
                    string fName = f.Substring(sourceDir.Length + 1);

                    try
                    {
                        // Will not overwrite if the destination file already exists.
                        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
                    }

                    // Catch exception if the file was already copied.
                    catch (IOException copyError)
                    {
                        MessageBox.Show(copyError.Message);
                    }
                }
            }
            catch (DirectoryNotFoundException dirNotFound)
            {
                MessageBox.Show(dirNotFound.Message);
            }

        } 

Comentários

Mensagens populares