C# 批量修改文件名

        private void button1_Click(object sender, EventArgs e)
        {
            //智能管家文件名修改Repository 把 aT_jMiscCustomerDetailRepository.cs 修改成 ATJMiscCustomerDetailRepository.cs

            //方法一
            //var cc = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //DirectoryInfo di = new DirectoryInfo(cc);
            //FileInfo[] files = di.GetFiles("*.cs", SearchOption.AllDirectories);
            //foreach (FileInfo fileinfo in files)
            //{
            //    string strNewFileName = "";
            //    string strOldFileName = "";
            //    strOldFileName = fileinfo.Name;
            //    string[] array2 = strOldFileName.Split(new char[] { '_' });
            //    for (int j = 0; j < array2.Length; j++)
            //    {
            //        string temp = array2[j];
            //        strNewFileName = strNewFileName + temp.Substring(0, 1).ToUpper() + temp.Substring(1, temp.Length - 1);
            //    }
            //    fileinfo.MoveTo(strNewFileName);
            //    //fileinfo.MoveTo(Path.GetFullPath(@"...") + "\" + strNewFileName);  //移动上级目录
            //    //Path.GetFullPath(@".")    输出(当前目录)   "F:\测试项目\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug"
            //    //Path.GetFullPath(@"..")    输出 (当前的上一级)  "F:\测试项目\WindowsFormsApplication1\WindowsFormsApplication1\bin"


            //    string path1 = new DirectoryInfo("./").FullName;  //输出 (当前目录)  "F:\测试项目\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug"
            //    string pathc = new DirectoryInfo("../").FullName;  //输出 (当前的上一级)  "F:\测试项目\WindowsFormsApplication1\WindowsFormsApplication1\bin"
            //}

            //方法二
            string currentDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string[] files = Directory.GetFiles(currentDirectory, "*.cs", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                string[] array = Path.GetFileName(files[i]).Split(new char[]
				{
					'_'
				});
                string text = "";
                string[] array2 = array;
                for (int j = 0; j < array2.Length; j++)
                {
                    string text2 = array2[j];
                    text = text + text2.Substring(0, 1).ToUpper() + text2.Substring(1, text2.Length - 1);
                }
                //File.Copy(files[i], Path.GetDirectoryName(files[i]) + "\" + text);
                File.Move(files[i], Path.GetDirectoryName(files[i]) + "\" + text);
                //    var c = Path.GetDirectoryName(@"C:mydirmyfile.ext");   //GetDirectoryName 返回指定路径字符串的目录信息。 输出 C:\mydir
            }
        }

  如果是asp.net教程就 Server.MapPath(".") 就可以获取。

 ./当前目录
/网站主目录
../上层目录
~/网站虚拟目录

如果当前的网站目录为E:wwwroot   应用程序虚拟目录为E:wwwrootcompany 浏览的页面路径为E:wwwrootcompany ewsshow.asp
在show.asp页面中使用
Server.MapPath("./")   返回路径为:E:wwwrootcompany ews
Server.MapPath("/")    返回路径为:E:wwwroot
Server.MapPath("../")   返回路径为:E:wwwrootcompany
Server.MapPath("~/")   返回路径为:E:wwwrootcompany
server.MapPath(request.ServerVariables("Path_Info")) 
Request.ServerVariables("Path_Translated")  
上面两种方式返回路径为 D:wwwrootcompany ewsshow.asp

下载 

原文地址:https://www.cnblogs.com/dare/p/8296076.html