using System;
using System.IO;
class Program
{
static void Main()
{
File.Copy("C:\\source.txt", "D:\\source.txt");
}
}
Code (C#)
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.");
}
}
}
Code (VB.NET)
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim path As String = "c:\temp\MyTest.txt"
Dim path2 As String = path + "temp"
Try
Dim fs As FileStream = File.Create(path)
fs.Close()
' 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 copying is not allowed, which was not expected.")
End Try
End Sub
End Class