C#修改文件或文件夹名称 .

来源:互联网 发布:阿里云企业邮箱免费版 编辑:程序博客网 时间:2024/05/18 00:05
上午把林春给的demo仔仔细细的调试了一遍,觉得有些明白了其中的原理。

    “数据是与界面呈现区分开的,一份数据可以有多种呈现形式”。

 

     已经从页面上获取了要修改的文件或者文件夹的名称,现在要做的就是要把数据传到后台页面进行处理了。

    查了一下msdn,里边没有Set文件夹名称的方法,但是提供了一个move的方法。这样的话,不是要在移动后再删除原来的数据吗?

 

    参考文档,做了个demo,move()方法其实就是重命名的。

    另外,使用DirectoryInfo中的moveto也是可以实现的。

    

    sourceCode:

 

            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);
            }
            

原创粉丝点击