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:\mydir\myfile.ext");   //GetDirectoryName 返回指定路徑字符串的目錄信息。 輸出 C:\\mydir
            }
        }

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

 ./當前目錄
/網站主目錄
../上層目錄
~/網站虛擬目錄

如果當前的網站目錄為E:\wwwroot   應用程序虛擬目錄為E:\wwwroot\company 瀏覽的頁面路徑為E:\wwwroot\company\news\show.asp
在show.asp頁面中使用
Server.MapPath("./")   返回路徑為:E:\wwwroot\company\news
Server.MapPath("/")    返回路徑為:E:\wwwroot
Server.MapPath("../")   返回路徑為:E:\wwwroot\company
Server.MapPath("~/")   返回路徑為:E:\wwwroot\company
server.MapPath(request.ServerVariables("Path_Info")) 
Request.ServerVariables("Path_Translated")  
上面兩種方式返回路徑為 D:\wwwroot\company\news\show.asp

下載 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM