C# rename步骤重命名文件

C# rename方法重命名文件

 

记得C# File类中是没有rename这个方法 所以网上很多都用的是move moveTo copy+delete等这些方法

 

其实以上的方法 虽然可以实现功能 但看起来总觉得很蛋疼

 

今天百度 突然发现了一个正宗的解决方案

 

仔细回想 vb.net中有My.Computer.FileSystem.RenameFile这个方法 但是在C#中如何使用呢?? 其实很简单 只需以下几步

 

1.先添加引用:Microsoft.VisualBasic,再加上using Microsoft.VisualBasic.Devices;
2.就下面两行

                Computer MyComputer = new Computer();
                MyComputer.FileSystem.RenameFile(FileName, newFileName);

 

其中FileName是你所要重命名的文件的全路径,newFileName仅仅是目标文件名;

 

http://blog.****.net/wenjunsu/article/details/5820634

1楼拿笔小心
这个的底层实现也是move file,蛋疼的方式不同而已,,public static void RenameFile(string file, string newName),{,file = FileSystem.NormalizeFilePath(file, quot;filequot;);,FileSystem.ThrowIfDevicePath(file);,if (!File.Exists(file)),{,throw ExceptionUtils.GetFileNotFoundException(file, quot;IO_FileNotFound_Pathquot;, new string[],{,file