c盘有个“bb.dat”文件,d盘也有个“bb.dat”文件,要将c盘的文件覆盖d盘的文件,
并保留c盘的文件,该怎么搞?

解决方案 »

  1.   

     File.Delete("D:\\bb.dat");
                File.Copy("C:\\bb.dat", "D:\\bb.dat");
               
      

  2.   


                FileInfo file = new FileInfo(@"c:\bb.dat");
                file.CopyTo(@"d:\bb.dat", true);
      

  3.   

    或者File.Copy(@"c:\bb.dat", @"d:\bb.dat", true);
      

  4.   

    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            string path = @"c:/temp/MyTest.txt";
            string path2 = path + "temp";        try 
            {
                // Create the file and clean up handles.
                using (FileStream fs = File.Create(path)) {}            // Ensure that the target does not exist.
                File.Delete(path2);            // Copy the file.
                File.Copy(path, path2);
                Console.WriteLine("{0} copied to {1}", path, path2);            // Try to copy the same file again, which should succeed.
                File.Copy(path, path2, true);
                Console.WriteLine("The second Copy operation succeeded, which was expected.");
            }         catch 
            {
                Console.WriteLine("Double copy is not allowed, which was not expected.");
            }
        }
    }
    [C#]  COPY TO
    using System;
    using System.IO;class Test 
    {
            public static void Main() 
        {
            string path = @"c:/temp/MyTest.txt";
            string path2 = @"c:/temp/MyTest.txt" + "temp";
            FileInfo fi1 = new FileInfo(path);
            FileInfo fi2 = new FileInfo(path2);        try 
            {
                // Create the file and clean up handles.
                using (FileStream fs = fi1.Create()) {}            //Ensure that the target does not exist.
                fi2.Delete();            //Copy the file.
                fi1.CopyTo(path2);
                Console.WriteLine("{0} was copied to {1}.", path, path2);            //Try to copy it again, which should succeed.
                fi1.CopyTo(path2, true);            Console.WriteLine("The second Copy operation succeeded, which is expected.");        } 
            catch 
            {
                Console.WriteLine("Double copying was not allowed, which is not expected.");
            }
        }
    }