C#IO操作之重命名目录操作
与操作文件 类似源码如下:
using System; using System.IO; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; namespace IOcaozuo { static class Program { public static void Rename(this DirectoryInfo dirInfo, string newName) { try { // "rename" it dirInfo.MoveTo(newName); } catch (IOException ioe) { // most likely given the directory exists or isn't empty Trace.WriteLine(ioe.ToString()); } } static void Main(string[] args) { RenameDirectory(); } public static void RenameDirectory() { string path = Path.GetTempPath() + "MyTemp"; string newpath = path + "1"; Directory.CreateDirectory(path); RenameDirectory(path, newpath); DirectoryInfo dirInfo = new DirectoryInfo(newpath); dirInfo.Rename(path); } public static void RenameDirectory(string originalName, string newName) { try { // "rename" it Directory.Move(originalName, newName); } catch (IOException ioe) { // most likely given the directory exists or isn't empty Console.WriteLine(ioe.ToString()); } } } } |
原创文章转载请注明出处:云飞扬IT的blog





