代码更改文件或文件夹名称

先看一段代码

        /// <summary>
        /// 
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        private bool RenameFilea(string oldName, string newName)
        {
            string filepath = System.IO.Directory.GetCurrentDirectory() + "\\" + oldName ;
            if (System.IO.Directory.Exists(filepath))
            {
                try
                {
                    string Savepath = System.IO.Directory.GetCurrentDirectory() + "\\" + newName ;
                    System.IO.Directory.Move(filepath, Savepath);
                    System.IO.Directory.Delete(filepath);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {          
                return false;
            }
        }

olderName指文件的原来名称;

newName指文件新的名称;

主要应用的

System.IO.Directory.Move(filepath, Savepath);

System.IO.Directory.Delete(filepath);

这两个方法。

转载于:https://www.cnblogs.com/springyangwc/archive/2011/02/28/1967212.html

猜你喜欢

转载自blog.csdn.net/weixin_33802505/article/details/93340832
今日推荐