File.Copy メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
既存のファイルを新しいファイルにコピーします。
オーバーロード
Copy(String, String, Boolean) |
既存のファイルを新しいファイルにコピーします。 同じ名前のファイルの上書きが許可されます。 |
Copy(String, String) |
既存のファイルを新しいファイルにコピーします。 同じ名前のファイルを上書きできません。 |
Copy(String, String, Boolean)
既存のファイルを新しいファイルにコピーします。 同じ名前のファイルの上書きが許可されます。
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
は読み取り専用です。または -
overwrite
はtrue
です。destFileName
は存在するのに非表示ですが、sourceFileName
は非表示になっていません。
sourceFileName
または destFileName
は長さ 0 の文字列で、空白のみで構成されているか、正しくない文字を含んでいます。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
- または -
sourceFileName
またはdestFileName
がディレクトリを指定しています。
sourceFileName
または destFileName
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
sourceFileName
または destFileName
で指定されたパスが正しくありません (マップされていないドライブ上のパスなど)。
sourceFileName
が見つかりませんでした。
sourceFileName
または destFileName
の形式が正しくありません。
例
次の例では、C:\archives\2008 バックアップ フォルダーにファイルをコピーします。 メソッドの 2 つのオーバーロードを Copy 次のように使用します。
- 最初に、メソッド オーバーロードを 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);
}
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 タスク を参照してください。
こちらもご覧ください
- Move(String, String)
- Move(String, String)
- ファイルおよびストリーム入出力
- ファイルからのテキストの読み取り
- 方法:ファイルにテキストを書き込む
- 方法:新しく作成されたデータ ファイルに対して読み書きする
適用対象
Copy(String, String)
既存のファイルを新しいファイルにコピーします。 同じ名前のファイルを上書きできません。
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 の文字列で、空白のみで構成されているか、正しくない文字を含んでいます。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
- または -
sourceFileName
またはdestFileName
がディレクトリを指定しています。
sourceFileName
または destFileName
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
sourceFileName
または destFileName
で指定されたパスが正しくありません (マップされていないドライブ上のパスなど)。
sourceFileName
が見つかりませんでした。
sourceFileName
または destFileName
の形式が正しくありません。
例
次の例では、C:\archives\2008 バックアップ フォルダーにファイルをコピーします。 メソッドの 2 つのオーバーロードを Copy 次のように使用します。
最初に、メソッド オーバーロードを 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);
}
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)設定false
されたoverwrite
メソッド オーバーロードと同じです。
パラメーターはsourceFileName``destFileName
、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、メソッドを Directory.GetCurrentDirectory 参照してください。 このメソッドでは、パラメーター内のワイルドカード文字はサポートされていません。
元のファイルの属性は、コピーされたファイルに保持されます。
こちらもご覧ください
- Move(String, String)
- Move(String, String)
- ファイルおよびストリーム入出力
- ファイルからのテキストの読み取り
- 方法:ファイルにテキストを書き込む
- 方法:新しく作成されたデータ ファイルに対して読み書きする