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.
Varolan bir dosyayı yeni bir dosyaya kopyalar.
Aşırı Yüklemeler
Copy(String, String, Boolean) |
Varolan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazmaya izin verilir. |
Copy(String, String) |
Varolan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılması yasaktır. |
Copy(String, String, Boolean)
- Kaynak:
- File.cs
- Kaynak:
- File.cs
- Kaynak:
- File.cs
Varolan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazmaya 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
true
hedef dosyanın zaten varsa değiştirilmesi gerekiyorsa; 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 uzunluklu bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.
-veya-
sourceFileName
veya destFileName
bir dizin belirtir.
sourceFileName
veya destFileName
şeklindedir null
.
Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.
veya destFileName
içinde sourceFileName
belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).
sourceFileName
bulunamadı.
sourceFileName
veya destFileName
geçersiz biçimdedir.
Örnekler
Aşağıdaki örnek dosyaları C:\archives\2008 yedekleme klasörüne kopyalar. Yönteminin iki aşırı yüklemesini Copy aşağıdaki gibi kullanır:
- İlk olarak metin (.txt) dosyalarını kopyalamak için yöntem aşırı yüklemesini kullanır File.Copy(String, String) . Kod, bu aşırı yüklemenin önceden kopyalanmış dosyaların üzerine yazılmasını sağlamadığını gösterir.
Daha sonra resimleri (.jpg dosyaları) kopyalamak için yöntem aşırı yüklemesini kullanır File.Copy(String, String, Boolean) . 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 bilgisi, 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 Stream G/Ç
- Dosyadan metin okuma
- Nasıl yapılır: Bir Dosyaya Metin Yazma
- Nasıl yapılır: Yeni Oluşturulan bir Veri Dosyasını Okuma ve Dosyaya Yazma
Şunlara uygulanır
Copy(String, String)
- Kaynak:
- File.cs
- Kaynak:
- File.cs
- Kaynak:
- File.cs
Varolan bir dosyayı yeni bir dosyaya kopyalar. Aynı ada sahip bir dosyanın üzerine yazılması yasaktır.
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 uzunluklu bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.
-veya-
sourceFileName
veya destFileName
bir dizin belirtir.
sourceFileName
veya destFileName
şeklindedir null
.
Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.
veya destFileName
içinde sourceFileName
belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).
sourceFileName
bulunamadı.
sourceFileName
veya destFileName
geçersiz biçimdedir.
Örnekler
Aşağıdaki örnek dosyaları C:\archives\2008 yedekleme klasörüne kopyalar. Yönteminin iki aşırı yüklemesini Copy aşağıdaki gibi kullanır:
İlk olarak metin (.txt) dosyalarını kopyalamak için yöntem aşırı yüklemesini kullanır File.Copy(String, String) . Kod, bu aşırı yüklemenin önceden kopyalanmış dosyaların üzerine yazılmasını sağlamadığını gösterir.
Daha sonra resimleri (.jpg dosyaları) kopyalamak için yöntem aşırı yüklemesini kullanır File.Copy(String, String, Boolean) . 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, parametresi olarak ayarlanmış false
yöntem aşırı yüklemesiyle overwrite
eşdeğerdirCopy(String, String, Boolean).
sourceFileName
ve destFileName
parametreleri göreli veya mutlak yol bilgilerini belirtebilir. Göreli yol bilgisi, geçerli çalışma dizinine göre yorumlanır. Geçerli çalışma dizinini almak için yöntemine Directory.GetCurrentDirectory 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 Stream G/Ç
- Dosyadan metin okuma
- Nasıl yapılır: Bir Dosyaya Metin Yazma
- Nasıl yapılır: Yeni Oluşturulan bir Veri Dosyasını Okuma ve Dosyaya Yazma
Şunlara uygulanır
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin