2009-03-25 44 views

Trả lời

82

Hãy xem FileInfo.

Đỗ một cái gì đó như thế này:

void RenameThem() 
{ 
    DirectoryInfo d = new DirectoryInfo("c:/dir/"); 
    FileInfo[] infos = d.GetFiles("*.myfiles"); 
    foreach(FileInfo f in infos) 
    { 
     // Do the renaming here 
     File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name)); 
    } 
} 
+15

sử dụng Path.Combine (f.Directory.ToString(), "1" + f.Name) thay vì "1" + f.FullName –

8

Chức năng mà bạn đang tìm kiếm là File.Move(source, destination) của namespace System.IO. Ngoài ra, hãy xem lớp DirectoryInfo (của cùng một không gian tên) để truy cập nội dung của thư mục.

1

Khám phá How can I rename a file in C#?. Tôi không biết rằng C# không có tên đổi tên ... Có vẻ như bạn phải sử dụng System.IO.File.Move(oldFileName, newFileName)

8

Tôi sẽ chỉ điền vào đây vì tôi cần viết mã này cho mục đích của riêng mình.

using System; 
using System.IO; 

public static class FileSystemInfoExtensions 
{ 
    public static void Rename(this FileSystemInfo item, string newName) 
    { 
     if (item == null) 
     { 
      throw new ArgumentNullException("item"); 
     } 

     FileInfo fileInfo = item as FileInfo; 
     if (fileInfo != null) 
     { 
      fileInfo.Rename(newName); 
      return; 
     } 

     DirectoryInfo directoryInfo = item as DirectoryInfo; 
     if (directoryInfo != null) 
     { 
      directoryInfo.Rename(newName); 
      return; 
     } 

     throw new ArgumentException("Item", "Unexpected subclass of FileSystemInfo " + item.GetType()); 
    } 

    public static void Rename(this FileInfo file, string newName) 
    { 
     // Validate arguments. 
     if (file == null) 
     { 
      throw new ArgumentNullException("file"); 
     } 
     else if (newName == null) 
     { 
      throw new ArgumentNullException("newName"); 
     } 
     else if (newName.Length == 0) 
     { 
      throw new ArgumentException("The name is empty.", "newName"); 
     } 
     else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 
      || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) 
     { 
      throw new ArgumentException("The name contains path separators. The file would be moved.", "newName"); 
     } 

     // Rename file. 
     string newPath = Path.Combine(file.DirectoryName, newName); 
     file.MoveTo(newPath); 
    } 

    public static void Rename(this DirectoryInfo directory, string newName) 
    { 
     // Validate arguments. 
     if (directory == null) 
     { 
      throw new ArgumentNullException("directory"); 
     } 
     else if (newName == null) 
     { 
      throw new ArgumentNullException("newName"); 
     } 
     else if (newName.Length == 0) 
     { 
      throw new ArgumentException("The name is empty.", "newName"); 
     } 
     else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 
      || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) 
     { 
      throw new ArgumentException("The name contains path separators. The directory would be moved.", "newName"); 
     } 

     // Rename directory. 
     string newPath = Path.Combine(directory.Parent.FullName, newName); 
     directory.MoveTo(newPath); 
    } 
} 
+2

Không có phương pháp đổi tên trên System.IO.FileInfo! – Chris

+3

@Chris Đó chính là lý do tại sao Journey đã viết phương thức 'Rename()' cho 'FileInfo'. – svick

+0

@svick doh! điều đó sẽ dạy tôi không cuộn xuống và đọc TẤT CẢ mã. Lỗi của tôi. – Chris

0

Bạn có thể sử dụng File.Move, như thế này:

string oldFilePath = Path.Combine(Server.MapPath("~/uploads"), "oldFileName"); 
string newFilePath = Path.Combine(Server.MapPath("~/uploads"), "newFileName"); 

File.Move(oldFilePath, newFilePath); 
0

On .NET Framework 4.0 Tôi sử dụng FileInfo.MoveTo() phương pháp mà chỉ mất 1 lập luận

Chỉ cần để di chuyển các file phương pháp của tôi trông như thế này

private void Move(string sourceDirName, string destDirName) 
{ 
    DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
    FileInfo[] files = null; 

    files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     string temppath = Path.Combine(destDirName, file.Name); 
     file.MoveTo(temppath); 
    } 
} 

để đổi tên tệp phương thức của tôi trông giống như thế này

private void Rename(string folderPath) 
{ 
    int fileCount = 0; 

    DirectoryInfo dir = new DirectoryInfo(folderPath); 

    files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     fileCount += 1; 
     string newFileName = fileCount.ToString() + file.Name; 
     string temppath = Path.Combine(folderPath, newFileName); 

     file.MoveTo(temppath); 
    } 
} 

AS bạn có thể thấy để Đổi tên tập tin nó cú pháp là gần như giống nhau như để di chuyển nó, chỉ cần phải sửa đổi filename trước khi sử dụng phương pháp MoveTo().

Các vấn đề liên quan