File.Copy Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Var olan bir dosyayı yeni bir dosyaya kopyalar.
Aşırı Yüklemeler
| Copy(String, String) |
Var olan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılmasına izin verilmez. |
| Copy(String, String, Boolean) |
Var olan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılmasına izin verilir. |
Copy(String, String)
- Kaynak:
- File.cs
- Kaynak:
- File.cs
- Kaynak:
- File.cs
Var olan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılmasına izin verilmez.
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)
Parametreler
- sourceFileName
- String
Kopyalanacak dosya.
- destFileName
- String
Hedef dosyanın adı. Bu bir dizin veya var olan bir dosya olamaz.
Özel durumlar
Çağıranın gerekli izni yok.
sourceFileName veya destFileName sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir.
GetInvalidPathChars() yöntemini kullanarak geçersiz karakterleri sorgulayabilirsiniz.
-veya-
sourceFileName veya destFileName bir dizin belirtir.
sourceFileName veya destFileNamenull.
Belirtilen yol, dosya adı veya her ikisi de sistem tanımlı uzunluk üst sınırını aşıyor.
sourceFileName veya destFileName belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).
sourceFileName bulunamadı.
sourceFileName veya destFileName geçersiz biçimde.
Örnekler
Aşağıdaki örnek dosyaları C:\archives\2008 yedekleme klasörüne kopyalar. Copy yönteminin iki aşırı yüklemesini aşağıdaki gibi kullanır:
İlk olarak metin (.txt) dosyalarını kopyalamak için File.Copy(String, String) yöntemi aşırı yüklemesini kullanır. Kod, bu aşırı yüklemenin zaten kopyalanmış dosyaların üzerine yazılmasına izin vermediğini gösterir.
Ardından resimleri kopyalamak için File.Copy(String, String, Boolean) yöntemi aşırı yüklemesini (.jpg dosyaları) kullanır. Kod, bu aşırı yüklemenin zaten kopyalanmış dosyaların üzerine yazılmasını sağlamadığını gösterir.
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);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"
try
let picList = Directory.GetFiles(sourceDir, "*.jpg")
let txtList = Directory.GetFiles(sourceDir, "*.txt")
// Copy picture files.
for f in picList do
// Remove path from the file name.
let 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.
for f in txtList do
// Remove path from the file name.
let 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.
with
| :? IOException as copyError -> printfn $"{copyError.Message}"
// Delete source files that were copied.
for f in txtList do
File.Delete f
for f in picList do
File.Delete f
// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{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
Açıklamalar
Bu yöntem, overwrite parametresi falseolarak ayarlanmış Copy(String, String, Boolean) yöntemi aşırı yüklemesiyle eşdeğerdir.
sourceFileName ve destFileName parametreleri göreli veya mutlak yol bilgilerini belirtebilir. Göreli yol bilgileri geçerli çalışma dizinine göre yorumlanır. Geçerli çalışma dizinini edinmek için Directory.GetCurrentDirectory yöntemine bakın. Bu yöntem, parametrelerdeki joker karakterleri desteklemez.
Özgün dosyanın öznitelikleri kopyalanan dosyada tutulur.
Ayrıca bkz.
- Move(String, String)
- Move(String, String)
- Dosya ve Akış G/Ç
- Dosyadan Metin Okuma
- Nasıl yapılır: Dosya Metin Yazma
- Nasıl yapılır: Yeni Oluşturulan Veri Dosyası Okuma ve Yazma
Şunlara uygulanır
Copy(String, String, Boolean)
- Kaynak:
- File.cs
- Kaynak:
- File.cs
- Kaynak:
- File.cs
Var olan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılmasına izin verilir.
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)
Parametreler
- sourceFileName
- String
Kopyalanacak dosya.
- destFileName
- String
Hedef dosyanın adı. Bu bir dizin olamaz.
- overwrite
- Boolean
Hedef dosyanın zaten varsa değiştirilmesi gerekip gerekmediğini true; aksi takdirde false.
Özel durumlar
Çağıranın gerekli izni yok.
-veya-
destFileName salt okunurdur.
-veya-
overwrite
true
destFileName var ve gizlidir, ancak sourceFileName gizli değildir.
sourceFileName veya destFileName sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir.
GetInvalidPathChars() yöntemini kullanarak geçersiz karakterleri sorgulayabilirsiniz.
-veya-
sourceFileName veya destFileName bir dizin belirtir.
sourceFileName veya destFileNamenull.
Belirtilen yol, dosya adı veya her ikisi de sistem tanımlı uzunluk üst sınırını aşıyor.
sourceFileName veya destFileName belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).
sourceFileName bulunamadı.
sourceFileName veya destFileName geçersiz biçimde.
Örnekler
Aşağıdaki örnek dosyaları C:\archives\2008 yedekleme klasörüne kopyalar. Copy yönteminin iki aşırı yüklemesini aşağıdaki gibi kullanır:
- İlk olarak metin (.txt) dosyalarını kopyalamak için File.Copy(String, String) yöntemi aşırı yüklemesini kullanır. Kod, bu aşırı yüklemenin zaten kopyalanmış dosyaların üzerine yazılmasına izin vermediğini gösterir.
Ardından resimleri kopyalamak için File.Copy(String, String, Boolean) yöntemi aşırı yüklemesini (.jpg dosyaları) kullanır. Kod, bu aşırı yüklemenin zaten kopyalanmış dosyaların üzerine yazılmasını sağlamadığını gösterir.
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);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"
try
let picList = Directory.GetFiles(sourceDir, "*.jpg")
let txtList = Directory.GetFiles(sourceDir, "*.txt")
// Copy picture files.
for f in picList do
// Remove path from the file name.
let 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.
for f in txtList do
// Remove path from the file name.
let 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.
with
| :? IOException as copyError -> printfn $"{copyError.Message}"
// Delete source files that were copied.
for f in txtList do
File.Delete f
for f in picList do
File.Delete f
// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{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
Açıklamalar
sourceFileName ve destFileName parametreleri göreli veya mutlak yol bilgilerini belirtebilir. Göreli yol bilgileri geçerli çalışma dizinine göre yorumlanır. Bu yöntem, parametrelerdeki joker karakterleri desteklemez.
Özgün dosyanın öznitelikleri kopyalanan dosyada tutulur.
Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.
Ayrıca bkz.
- Move(String, String)
- Move(String, String)
- Dosya ve Akış G/Ç
- Dosyadan Metin Okuma
- Nasıl yapılır: Dosya Metin Yazma
- Nasıl yapılır: Yeni Oluşturulan Veri Dosyası Okuma ve Yazma