FileInfo.CopyTo Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee kopieert u een bestaand bestand naar een nieuw bestand.
Overloads
| Name | Description |
|---|---|
| CopyTo(String) |
Hiermee kopieert u een bestaand bestand naar een nieuw bestand en wordt de toewijzing van het overschrijven van een bestaand bestand ongedaan gemaakt. |
| CopyTo(String, Boolean) |
Kopieert een bestaand bestand naar een nieuw bestand, waardoor een bestaand bestand kan worden overschreven. |
CopyTo(String)
Hiermee kopieert u een bestaand bestand naar een nieuw bestand en wordt de toewijzing van het overschrijven van een bestaand bestand ongedaan gemaakt.
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
Parameters
- destFileName
- String
De naam van het nieuwe bestand dat u wilt kopiëren.
Retouren
Een nieuw bestand met een volledig gekwalificeerd pad.
Uitzonderingen
.NET Framework en .NET Core-versies ouder dan 2.1: destFileName leeg is, alleen spaties bevat of ongeldige tekens bevat.
Er treedt een fout op of het doelbestand bestaat al.
De beller heeft niet de vereiste machtiging.
destFileName is null.
Er wordt een mappad doorgegeven of het bestand wordt verplaatst naar een ander station.
De map waarin is opgegeven destFileName , bestaat niet.
Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.
destFileName bevat een dubbele punt (:) binnen de tekenreeks, maar geeft het volume niet op.
Voorbeelden
In het volgende voorbeeld ziet u beide overbelastingen van de CopyTo methode.
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
In het volgende voorbeeld ziet u hoe u het ene bestand naar een ander bestand kopieert en een uitzondering genereert als het doelbestand al bestaat.
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...
Opmerkingen
Gebruik de CopyTo(String, Boolean) methode om het overschrijven van een bestaand bestand toe te staan.
Caution
Vermijd indien mogelijk korte bestandsnamen (zoals XXXXXX~1.XXX) met deze methode. Als twee bestanden gelijkwaardige korte bestandsnamen hebben, kan deze methode mislukken en een uitzondering veroorzaken en/of leiden tot ongewenst gedrag
Zie ook
- Bestands- en Stream-I/O
- Procedure: Tekst uit een bestand lezen
- Procedure: Tekst naar een bestand schrijven
- Procedure: lezen en schrijven naar een nieuw gegevensbestand
Van toepassing op
CopyTo(String, Boolean)
Kopieert een bestaand bestand naar een nieuw bestand, waardoor een bestaand bestand kan worden overschreven.
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
Parameters
- destFileName
- String
De naam van het nieuwe bestand dat u wilt kopiëren.
- overwrite
- Boolean
true om toe te staan dat een bestaand bestand wordt overschreven; anders, false.
Retouren
Een nieuw bestand of een overschrijf van een bestaand bestand als overwrite dat het is true. Als het bestand bestaat en overwrite is false, wordt er een IOException gegenereerd.
Uitzonderingen
.NET Framework en .NET Core-versies ouder dan 2.1: destFileName leeg is, alleen spaties bevat of ongeldige tekens bevat.
Er treedt een fout op of het doelbestand bestaat al en overwrite is false.
De beller heeft niet de vereiste machtiging.
destFileName is null.
De map waarin is opgegeven destFileName , bestaat niet.
Er wordt een mappad doorgegeven of het bestand wordt verplaatst naar een ander station.
Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.
destFileName bevat een dubbele punt (:) in het midden van de tekenreeks.
Voorbeelden
In het volgende voorbeeld ziet u beide overbelastingen van de CopyTo methode.
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
In het volgende voorbeeld ziet u hoe u het ene bestand naar een ander bestand kopieert en aangeeft of u een bestand wilt overschrijven dat al bestaat.
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...
'
Opmerkingen
Gebruik deze methode om het overschrijven van een bestaand bestand toe te staan of te voorkomen. Gebruik de CopyTo(String) methode om te voorkomen dat een bestaand bestand standaard wordt overschreven.
Caution
Vermijd indien mogelijk korte bestandsnamen (zoals XXXXXX~1.XXX) met deze methode. Als twee bestanden gelijkwaardige korte bestandsnamen hebben, kan deze methode mislukken en een uitzondering veroorzaken en/of leiden tot ongewenst gedrag
Zie ook
- Bestands- en Stream-I/O
- Procedure: Tekst uit een bestand lezen
- Procedure: Tekst naar een bestand schrijven
- Procedure: lezen en schrijven naar een nieuw gegevensbestand