FileInfo.CopyTo Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Kopiert eine vorhandene Datei in eine neue Datei.
Überlädt
| Name | Beschreibung |
|---|---|
| CopyTo(String) |
Kopiert eine vorhandene Datei in eine neue Datei, wobei das Überschreiben einer vorhandenen Datei deaktiviert wird. |
| CopyTo(String, Boolean) |
Kopiert eine vorhandene Datei in eine neue Datei, sodass eine vorhandene Datei überschrieben wird. |
CopyTo(String)
Kopiert eine vorhandene Datei in eine neue Datei, wobei das Überschreiben einer vorhandenen Datei deaktiviert wird.
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
Parameter
- destFileName
- String
Der Name der neuen Zu kopierenden Datei.
Gibt zurück
Eine neue Datei mit einem vollqualifizierten Pfad.
Ausnahmen
.NET Framework- und .NET Core-Versionen, die älter als 2.1 sind: destFileName ist leer, enthält nur Leerzeichen oder ungültige Zeichen.
Ein Fehler tritt auf, oder die Zieldatei ist bereits vorhanden.
Der Aufrufer verfügt nicht über die erforderliche Berechtigung.
destFileName ist null.
Ein Verzeichnispfad wird übergeben, oder die Datei wird auf ein anderes Laufwerk verschoben.
Das angegebene destFileName Verzeichnis ist nicht vorhanden.
Der angegebene Pfad, der Dateiname oder beide überschreiten die vom System definierte maximale Länge.
destFileName enthält einen Doppelpunkt (:) innerhalb der Zeichenfolge, gibt jedoch nicht das Volume an.
Beispiele
Im folgenden Beispiel werden beide Überladungen der CopyTo Methode veranschaulicht.
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
Das folgende Beispiel veranschaulicht das Kopieren einer Datei in eine andere Datei und löst eine Ausnahme aus, wenn die Zieldatei bereits vorhanden ist.
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...
Hinweise
Verwenden Sie die CopyTo(String, Boolean) Methode, um das Überschreiben einer vorhandenen Datei zu ermöglichen.
Vorsicht
Vermeiden Sie möglichst kurze Dateinamen (z. B. XXXXXX~1.XXX) mit dieser Methode. Wenn zwei Dateien gleichwertige kurze Dateinamen haben, schlägt diese Methode möglicherweise fehl und löst eine Ausnahme aus und/oder führt zu unerwünschtem Verhalten.
Weitere Informationen
- Datei- und Stream-E/A
- Vorgehensweise: Lesen von Text aus einer Datei
- Vorgehensweise: Schreiben von Text in eine Datei
- Vorgehensweise: Lesen und Schreiben in eine neu erstellte Datendatei
Gilt für:
CopyTo(String, Boolean)
Kopiert eine vorhandene Datei in eine neue Datei, sodass eine vorhandene Datei überschrieben wird.
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
Parameter
- destFileName
- String
Der Name der neuen Zu kopierenden Datei.
- overwrite
- Boolean
truezuzulassen, dass eine vorhandene Datei überschrieben werden kann; andernfalls . false
Gibt zurück
Eine neue Datei oder ein Überschreiben einer vorhandenen Datei, falls overwrite vorhanden.true Wenn die Datei vorhanden ist und overwrite ist false, wird ein IOException Fehler ausgelöst.
Ausnahmen
.NET Framework- und .NET Core-Versionen, die älter als 2.1 sind: destFileName ist leer, enthält nur Leerzeichen oder ungültige Zeichen.
Ein Fehler tritt auf, oder die Zieldatei ist bereits vorhanden und overwrite ist false.
Der Aufrufer verfügt nicht über die erforderliche Berechtigung.
destFileName ist null.
Das angegebene destFileName Verzeichnis ist nicht vorhanden.
Ein Verzeichnispfad wird übergeben, oder die Datei wird auf ein anderes Laufwerk verschoben.
Der angegebene Pfad, der Dateiname oder beide überschreiten die vom System definierte maximale Länge.
destFileName enthält einen Doppelpunkt (:) in der Mitte der Zeichenfolge.
Beispiele
Im folgenden Beispiel werden beide Überladungen der CopyTo Methode veranschaulicht.
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
Das folgende Beispiel veranschaulicht das Kopieren einer Datei in eine andere Datei und gibt an, ob eine datei überschrieben werden soll, die bereits vorhanden ist.
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...
'
Hinweise
Verwenden Sie diese Methode, um das Überschreiben einer vorhandenen Datei zuzulassen oder zu verhindern. Verwenden Sie die CopyTo(String) Methode, um das Überschreiben einer vorhandenen Datei standardmäßig zu verhindern.
Vorsicht
Vermeiden Sie möglichst kurze Dateinamen (z. B. XXXXXX~1.XXX) mit dieser Methode. Wenn zwei Dateien gleichwertige kurze Dateinamen haben, schlägt diese Methode möglicherweise fehl und löst eine Ausnahme aus und/oder führt zu unerwünschtem Verhalten.
Weitere Informationen
- Datei- und Stream-E/A
- Vorgehensweise: Lesen von Text aus einer Datei
- Vorgehensweise: Schreiben von Text in eine Datei
- Vorgehensweise: Lesen und Schreiben in eine neu erstellte Datendatei