File.Copy メソッド

既存のファイルを新しいファイルにコピーします。

オーバーロードの一覧

既存のファイルを新しいファイルにコピーします。同じ名前のファイルを上書きできません。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Sub Copy(String, String)

[C#] public static void Copy(string, string);

[C++] public: static void Copy(String*, String*);

[JScript] public static function Copy(String, String);

既存のファイルを新しいファイルにコピーします。同じ名前のファイルの上書きが許可されます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Sub Copy(String, String, Boolean)

[C#] public static void Copy(string, string, bool);

[C++] public: static void Copy(String*, String*, bool);

[JScript] public static function Copy(String, String, Boolean);

使用例

[Visual Basic, C#, C++] 同じ名前のコピー先ファイルの上書きを許可して、ファイルを指定したパスにコピーする例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、Copy のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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

[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.");
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    String* path2 = String::Concat(path, S"temp");

    try {
        // Create the file and clean up handles.
        IDisposable* file = __try_cast<IDisposable*>(File::Create(path));
        if (file) file->Dispose();

        // Ensure that the target does not exist.
        File::Delete(path2);

        // Copy the file.
        File::Copy(path, path2);
        Console::WriteLine(S"{0} copied to {1}", path, path2);

        // Try to copy the same file again, which should succeed.
        File::Copy(path, path2, true);
        Console::WriteLine(S"The second Copy operation succeeded, which was expected.");
    } catch (Exception*) {
        Console::WriteLine(S"Double copy is not allowed, which was not expected.");
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

File クラス | File メンバ | System.IO 名前空間