FileInfo.CopyTo Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Kopierar en befintlig fil till en ny fil.
Överlagringar
| Name | Description |
|---|---|
| CopyTo(String) |
Kopierar en befintlig fil till en ny fil och tillåter inte att en befintlig fil skrivs över. |
| CopyTo(String, Boolean) |
Kopierar en befintlig fil till en ny fil, vilket gör att en befintlig fil kan skrivas över. |
CopyTo(String)
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
Kopierar en befintlig fil till en ny fil och tillåter inte att en befintlig fil skrivs över.
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
Parametrar
- destFileName
- String
Namnet på den nya filen som ska kopieras till.
Returer
En ny fil med en fullständigt kvalificerad sökväg.
Undantag
.NET Framework- och .NET Core-versioner som är äldre än 2.1: destFileName är tom, innehåller endast blanksteg eller innehåller ogiltiga tecken.
Ett fel inträffar, eller så finns målfilen redan.
Anroparen har inte den behörighet som krävs.
destFileName är null.
En katalogsökväg skickas eller så flyttas filen till en annan enhet.
Katalogen som anges i destFileName finns inte.
Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.
destFileName innehåller ett kolon (:) i strängen men anger inte volymen.
Exempel
I följande exempel visas båda överlagringarna av CopyTo metoden.
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
I följande exempel visas hur du kopierar en fil till en annan fil och utlöser ett undantag om målfilen redan finns.
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...
Kommentarer
Använd metoden CopyTo(String, Boolean) för att tillåta överskrivning av en befintlig fil.
Caution
Undvik att använda korta filnamn (till exempel XXXXXX~1.XXX) med den här metoden när det är möjligt. Om två filer har motsvarande korta filnamn kan den här metoden misslyckas och generera ett undantag och/eller resultera i oönskat beteende
Se även
- Fil- och ström-I/O
- Anvisningar: Läsa text från en fil
- Anvisningar: Skriva text till en fil
- Anvisningar: Läsa och skriva till en nyligen skapad datafil
Gäller för
CopyTo(String, Boolean)
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
- Källa:
- FileInfo.cs
Kopierar en befintlig fil till en ny fil, vilket gör att en befintlig fil kan skrivas över.
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
Parametrar
- destFileName
- String
Namnet på den nya filen som ska kopieras till.
- overwrite
- Boolean
trueför att tillåta att en befintlig fil skrivs över; annars . false
Returer
En ny fil eller en överskrivning av en befintlig fil om overwrite är true. Om filen finns och overwrite är falsegenereras en IOException .
Undantag
.NET Framework- och .NET Core-versioner som är äldre än 2.1: destFileName är tom, innehåller endast blanksteg eller innehåller ogiltiga tecken.
Ett fel inträffar, eller så finns målfilen redan och overwrite är false.
Anroparen har inte den behörighet som krävs.
destFileName är null.
Katalogen som anges i destFileName finns inte.
En katalogsökväg skickas eller så flyttas filen till en annan enhet.
Den angivna sökvägen, filnamnet eller båda överskrider den systemdefinierade maximala längden.
destFileName innehåller ett kolon (:) i mitten av strängen.
Exempel
I följande exempel visas båda överlagringarna av CopyTo metoden.
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
I följande exempel visas hur du kopierar en fil till en annan fil och anger om du vill skriva över en fil som redan finns.
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...
'
Kommentarer
Använd den här metoden för att tillåta eller förhindra överskrivning av en befintlig fil. CopyTo(String) Använd metoden för att förhindra att en befintlig fil skrivs över som standard.
Caution
Undvik att använda korta filnamn (till exempel XXXXXX~1.XXX) med den här metoden när det är möjligt. Om två filer har motsvarande korta filnamn kan den här metoden misslyckas och generera ett undantag och/eller resultera i oönskat beteende
Se även
- Fil- och ström-I/O
- Anvisningar: Läsa text från en fil
- Anvisningar: Skriva text till en fil
- Anvisningar: Läsa och skriva till en nyligen skapad datafil