FileInfo file = new FileInfo(@"c:\bb.dat"); file.CopyTo(@"d:\bb.dat", true);
或者File.Copy(@"c:\bb.dat", @"d:\bb.dat", true);
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."); } } }
File.Copy("C:\\bb.dat", "D:\\bb.dat");
FileInfo file = new FileInfo(@"c:\bb.dat");
file.CopyTo(@"d:\bb.dat", true);
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.");
}
}
}