.net修改文件或文件夹名称




在写C#语句的时候经常会遇到要修改某个文件夹的名称或者某个文件的名称,所以就有以下几种经常用的方法:

路径都是明确的路径;


string srcFileName = @"D:/a.txt";
string destFileName = @"D:/b.txt";
string srcFolderPath = @"D:/1";
string destFolderPath = @"D:/6";


方法一:

 //文件
if (System.IO.File.Exists(srcFileName))
{
   System.IO.File.Move(srcFileName, destFileName);
}
//文件夹
if (System.IO.Directory.Exists(srcFolderPath))
{
  System.IO.Directory.Move(srcFolderPath, destFolderPath);
}


方法二:


 //文件
if (System.IO.File.Exists(srcFileName))
{
  System.IO.FileInfo file = new System.IO.FileInfo(srcFileName);
  file.MoveTo(destFileName);
}
            
//文件夹
if (System.IO.Directory.Exists(srcFolderPath))
{
    System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(srcFolderPath);
    folder.MoveTo(destFolderPath);
}

目前用的是以上两种比较简单的C#方法法来对文件夹或者文件进行名称的修改


检查文件是否存在不存在则建立

if (!Directory.Exists(路径))
{   
 Directory.CreateDirectory(路径);
}


打赏

取消 我去学网

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少
微信

打开微信扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

  1. #2

    木庄网络博客(2016/10/28 11:41:03)
    不错的网站主题,看着相当舒服

  2. #1

    木庄网络博客(2016/10/14 21:02:39)
    博客做得好漂亮哦!