FileInfo.CopyTo 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將現有的檔案複製到新的檔案。
多載
| 名稱 | Description |
|---|---|
| CopyTo(String) |
將現有的檔案複製到新的檔案,不允許覆寫現有的檔案。 |
| CopyTo(String, Boolean) |
將現有的檔案複製到新的檔案,允許覆寫現有的檔案。 |
CopyTo(String)
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
將現有的檔案複製到新的檔案,不允許覆寫現有的檔案。
public:
System::IO::FileInfo ^ CopyTo(System::String ^ destFileName);
public System.IO.FileInfo CopyTo(string destFileName);
member this.CopyTo : string -> System.IO.FileInfo
Public Function CopyTo (destFileName As String) As FileInfo
參數
- destFileName
- String
要複製的新檔案名稱。
傳回
一個帶有完整限定路徑的新檔案。
例外狀況
.NET Framework 及 2.1 .NET Core 版本:destFileName 為空、僅有空白或無效字元。
發生錯誤,或目標檔案已經存在。
來電者沒有所需的權限。
destFileName 是 null。
會傳遞目錄路徑,或是檔案被移到另一個磁碟。
該 destFileName 目錄不存在。
指定的路徑、檔名或兩者都超過系統定義的最大長度。
destFileName 字串中包含冒號(:),但未指定音量。
範例
以下範例展示了該 CopyTo 方法的兩種過載。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\SoureFile.txt";
string path2 = @"c:\NewFile.txt";
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);
try
{
// Create the source file.
using (FileStream fs = fi1.Create()) { }
//Ensure that the target file does not exist.
if (File.Exists(path2))
{
fi2.Delete();
}
//Copy the file.f
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
'Specify the directories you want to manipulate.
Dim path As String = "c:\SourceFile.txt"
Dim path2 As String = "c:\NewFile.txt"
Dim fi As FileInfo = New FileInfo(path)
Dim fi2 As FileInfo = New FileInfo(path2)
Try
Using fs As FileStream = fi.Create()
End Using
'Ensure that the target does not exist.
If File.Exists(path2) Then
fi2.Delete()
End If
'Copy the file.
fi.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
Catch ioex As IOException
Console.WriteLine(ioex.Message)
End Try
End Sub
End Class
以下範例示範將一個檔案複製到另一個檔案,若目標檔案已存在則拋出例外。
using System;
using System.IO;
public class CopyToTest
{
public static void Main()
{
try
{
// Create a reference to a file, which might or might not exist.
// If it does not exist, it is not yet created.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("Add as many lines as you like...");
sw.WriteLine("Add another line to the output...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
StreamReader sr = new StreamReader(fi.OpenRead());
Console.WriteLine("This is the information in the first file:");
while (sr.Peek() != -1)
Console.WriteLine(sr.ReadLine());
// Copy this file to another file. The file will not be overwritten if it already exists.
FileInfo newfi = fi.CopyTo("newTemp.txt");
// Get the information out of the new file and display it.
sr = new StreamReader(newfi.OpenRead());
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine);
while (sr.Peek() != -1)
Console.WriteLine(sr.ReadLine());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is the information in the first file:
//Add as many lines as you like...
//Add another line to the output...
//This is the information in the second file:
//Add as many lines as you like...
//Add another line to the output...
Imports System.IO
Public Class CopyToTest
Public Shared Sub Main()
Try
' Create a reference to a file, which might or might not exist.
' If it does not exist, it is not yet created.
Dim fi As New FileInfo("temp.txt")
' Create a writer, ready to add entries to the file.
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("Add as many lines as you like...")
sw.WriteLine("Add another line to the output...")
sw.Flush()
sw.Close()
' Get the information out of the file and display it.
Dim sr As New StreamReader(fi.OpenRead())
Console.WriteLine("This is the information in the first file:")
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
' Copy this file to another file.
Dim newfi As FileInfo = fi.CopyTo("newTemp.txt")
' Get the information out of the new file and display it.
sr = New StreamReader(newfi.OpenRead())
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine)
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is the information in the first file:
'Add as many lines as you like...
'Add another line to the output...
'
'This is the information in the second file:
'Add as many lines as you like...
'Add another line to the output...
備註
使用此 CopyTo(String, Boolean) 方法允許覆蓋現有檔案。
注意事項
盡可能避免使用此方法使用短檔名(例如 XXXXXX~1.XXX)。 若兩個檔案的短檔名稱相同,此方法可能會失敗並觸發例外及/或造成不良行為
另請參閱
適用於
CopyTo(String, Boolean)
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
- 來源:
- FileInfo.cs
將現有的檔案複製到新的檔案,允許覆寫現有的檔案。
public:
System::IO::FileInfo ^ CopyTo(System::String ^ destFileName, bool overwrite);
public System.IO.FileInfo CopyTo(string destFileName, bool overwrite);
member this.CopyTo : string * bool -> System.IO.FileInfo
Public Function CopyTo (destFileName As String, overwrite As Boolean) As FileInfo
參數
- destFileName
- String
要複製的新檔案名稱。
- overwrite
- Boolean
true允許覆蓋現有檔案;否則,。 false
傳回
若為 ,則為新檔案,或若為 overwrite則覆蓋現有檔案true。 若檔案存在且 overwrite 為 false,則拋出 an IOException 。
例外狀況
.NET Framework 及 2.1 .NET Core 版本:destFileName 為空、僅有空白或無效字元。
發生錯誤,或目標檔案已存在且 overwrite 為 false。
來電者沒有所需的權限。
destFileName 是 null。
該 destFileName 目錄不存在。
會傳遞目錄路徑,或是檔案被移到另一個磁碟。
指定的路徑、檔名或兩者都超過系統定義的最大長度。
destFileName 包含一個冒號(:)位於弦的中間。
範例
以下範例展示了該 CopyTo 方法的兩種過載。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\SoureFile.txt";
string path2 = @"c:\NewFile.txt";
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);
try
{
// Create the source file.
using (FileStream fs = fi1.Create()) { }
//Ensure that the target file does not exist.
if (File.Exists(path2))
{
fi2.Delete();
}
//Copy the file.f
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
}
Imports System.IO
Public Class Test
Public Shared Sub Main()
'Specify the directories you want to manipulate.
Dim path As String = "c:\SourceFile.txt"
Dim path2 As String = "c:\NewFile.txt"
Dim fi As FileInfo = New FileInfo(path)
Dim fi2 As FileInfo = New FileInfo(path2)
Try
Using fs As FileStream = fi.Create()
End Using
'Ensure that the target does not exist.
If File.Exists(path2) Then
fi2.Delete()
End If
'Copy the file.
fi.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
Catch ioex As IOException
Console.WriteLine(ioex.Message)
End Try
End Sub
End Class
以下範例示範如何將一個檔案複製到另一個檔案,並指定是否覆蓋已存在的檔案。
using System;
using System.IO;
public class CopyToTest
{
public static void Main()
{
// Create a reference to a file, which might or might not exist.
// If it does not exist, it is not yet created.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("Add as many lines as you like...");
sw.WriteLine("Add another line to the output...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
StreamReader sr = new StreamReader( fi.OpenRead() );
Console.WriteLine("This is the information in the first file:");
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
// Copy this file to another file. The true parameter specifies
// that the file will be overwritten if it already exists.
FileInfo newfi = fi.CopyTo("newTemp.txt", true);
// Get the information out of the new file and display it.
sr = new StreamReader( newfi.OpenRead() );
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine);
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is the information in the first file:
//Add as many lines as you like...
//Add another line to the output...
//Add as many lines as you like...
//Add another line to the output...
//This is the information in the second file:
//Add as many lines as you like...
//Add another line to the output...
//Add as many lines as you like...
//Add another line to the output...
Imports System.IO
Public Class CopyToTest
Public Shared Sub Main()
' Create a reference to a file, which might or might not exist.
' If it does not exist, it is not yet created.
Dim fi As New FileInfo("temp.txt")
' Create a writer, ready to add entries to the file.
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("Add as many lines as you like...")
sw.WriteLine("Add another line to the output...")
sw.Flush()
sw.Close()
' Get the information out of the file and display it.
Dim sr As New StreamReader(fi.OpenRead())
Console.WriteLine("This is the information in the first file:")
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
' Copy this file to another file. The true parameter specifies
' that the file will be overwritten if it already exists.
Dim newfi As FileInfo = fi.CopyTo("newTemp.txt", True)
' Get the information out of the new file and display it.
sr = New StreamReader(newfi.OpenRead())
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine)
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is the information in the first file:
'Add as many lines as you like...
'Add another line to the output...
'
'This is the information in the second file:
'Add as many lines as you like...
'Add another line to the output...
'
備註
使用此方法可允許或防止覆蓋現有檔案。 使用該 CopyTo(String) 方法可避免預設覆蓋現有檔案。
注意事項
盡可能避免使用此方法使用短檔名(例如 XXXXXX~1.XXX)。 若兩個檔案的短檔名稱相同,此方法可能會失敗並觸發例外及/或造成不良行為