File.Copy メソッド

定義

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

オーバーロード

名前 説明
Copy(String, String)

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

Copy(String, String, Boolean)

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

Copy(String, String)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

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

public:
 static void Copy(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Copy(string sourceFileName, string destFileName);
static member Copy : string * string -> unit
Public Shared Sub Copy (sourceFileName As String, destFileName As String)

パラメーター

sourceFileName
String

コピーするファイル。

destFileName
String

コピー先ファイルの名前。 ディレクトリまたは既存のファイルにすることはできません。

例外

呼び出し元に必要なアクセス許可がありません。

sourceFileName または destFileName が長さ 0 の文字列であるか、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

-又は-

sourceFileName または destFileName ディレクトリを指定します。

sourceFileName または destFileNamenull

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

sourceFileNameまたはdestFileNameで指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

sourceFileName が見つかりませんでした。

destFileName 存在。

-又は-

I/O エラーが発生しました。

sourceFileName または destFileName が無効な形式です。

次の例では、C:\archives\2008 バックアップ フォルダーにファイルをコピーします。 次のように、 Copy メソッドの 2 つのオーバーロードを使用します。

  • 最初に、 File.Copy(String, String) メソッドのオーバーロードを使用してテキスト (.txt) ファイルをコピーします。 このコードは、このオーバーロードによって、既にコピーされたファイルの上書きが許可されていないことを示しています。

  • 次に、 File.Copy(String, String, Boolean) メソッドのオーバーロードを使用して画像 (.jpg ファイル) をコピーします。 このコードは、このオーバーロードによって、既にコピーされたファイルの上書きが許可されることを示しています。

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

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

        // 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)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"

try
    let picList = Directory.GetFiles(sourceDir, "*.jpg")
    let txtList = Directory.GetFiles(sourceDir, "*.txt")

    // Copy picture files.
    for f in picList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)

    // Copy text files.
    for f in txtList do
        // Remove path from the file name.
        let 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.
        with
        | :? IOException as copyError -> printfn $"{copyError.Message}"

    // Delete source files that were copied.
    for f in txtList do
        File.Delete f

    for f in picList do
        File.Delete f

// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"

Try
    Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
    Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")

    ' Copy picture files.
    For Each f As String In picList
        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        ' Use the Path.Combine method to safely append the file name to the path.
        ' Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
    Next

    ' Copy text files.
    For Each f As String In txtList

        'Remove path from the file name.
        Dim fName As String = 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 copyError As IOException
            Console.WriteLine(copyError.Message)
        End Try
    Next

    For Each f As String In txtList
        File.Delete(f)
    Next

    For Each f As String In picList
        File.Delete(f)
    Next

Catch dirNotFound As DirectoryNotFoundException
    Console.WriteLine(dirNotFound.Message)
End Try

注釈

このメソッドは、Copy(String, String, Boolean) パラメーターを overwrite に設定したfalse メソッドのオーバーロードと同じです。

sourceFileNameおよびdestFileNameパラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 Directory.GetCurrentDirectory メソッドを参照してください。 このメソッドは、パラメーター内のワイルドカード文字をサポートしていません。

元のファイルの属性は、コピーされたファイルに保持されます。

こちらもご覧ください

適用対象

Copy(String, String, Boolean)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

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

public:
 static void Copy(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Copy(string sourceFileName, string destFileName, bool overwrite);
static member Copy : string * string * bool -> unit
Public Shared Sub Copy (sourceFileName As String, destFileName As String, overwrite As Boolean)

パラメーター

sourceFileName
String

コピーするファイル。

destFileName
String

コピー先ファイルの名前。 ディレクトリにすることはできません。

overwrite
Boolean

true コピー先ファイルが既に存在する場合は置き換える必要がある場合。それ以外の場合は false

例外

呼び出し元に必要なアクセス許可がありません。

-又は-

destFileName は読み取り専用です。

-又は-

overwritetruedestFileName が存在し、非表示になっていますが、 sourceFileName は非表示ではありません。

sourceFileName または destFileName が長さ 0 の文字列であるか、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

-又は-

sourceFileName または destFileName ディレクトリを指定します。

sourceFileName または destFileNamenull

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

sourceFileNameまたはdestFileNameで指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

sourceFileName が見つかりませんでした。

destFileName が存在し、 overwritefalse

-又は-

I/O エラーが発生しました。

sourceFileName または destFileName が無効な形式です。

次の例では、C:\archives\2008 バックアップ フォルダーにファイルをコピーします。 次のように、 Copy メソッドの 2 つのオーバーロードを使用します。

  • 最初に、 File.Copy(String, String) メソッドのオーバーロードを使用してテキスト (.txt) ファイルをコピーします。 このコードは、このオーバーロードによって、既にコピーされたファイルの上書きが許可されていないことを示しています。

次に、 File.Copy(String, String, Boolean) メソッドのオーバーロードを使用して画像 (.jpg ファイル) をコピーします。 このコードは、このオーバーロードによって、既にコピーされたファイルの上書きが許可されることを示しています。

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

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

        // 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)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"

try
    let picList = Directory.GetFiles(sourceDir, "*.jpg")
    let txtList = Directory.GetFiles(sourceDir, "*.txt")

    // Copy picture files.
    for f in picList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)

    // Copy text files.
    for f in txtList do
        // Remove path from the file name.
        let 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.
        with
        | :? IOException as copyError -> printfn $"{copyError.Message}"

    // Delete source files that were copied.
    for f in txtList do
        File.Delete f

    for f in picList do
        File.Delete f

// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"

Try
    Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
    Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")

    ' Copy picture files.
    For Each f As String In picList
        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        ' Use the Path.Combine method to safely append the file name to the path.
        ' Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
    Next

    ' Copy text files.
    For Each f As String In txtList

        'Remove path from the file name.
        Dim fName As String = 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 copyError As IOException
            Console.WriteLine(copyError.Message)
        End Try
    Next

    For Each f As String In txtList
        File.Delete(f)
    Next

    For Each f As String In picList
        File.Delete(f)
    Next

Catch dirNotFound As DirectoryNotFoundException
    Console.WriteLine(dirNotFound.Message)
End Try

注釈

sourceFileNameおよびdestFileNameパラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 このメソッドは、パラメーター内のワイルドカード文字をサポートしていません。

元のファイルの属性は、コピーされたファイルに保持されます。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象