C# 文件/文件夾重命名


C# 重命名的方法是MoveTo()

官方文檔地址 (https://msdn.microsoft.com/zh-cn/library/system.io.fileinfo.moveto%28VS.80%29.aspx)

另附上Demo部分代碼:

 private void btn_start_Click(object sender, EventArgs e)
        {
            //找到目錄
            DirectoryInfo directoryinfo = new DirectoryInfo(txt_path.Text);
            int i = 1;
            //讀取當前目錄文件信息
            foreach (var item in directoryinfo.GetFiles())
            {
              
                string destPath = Path.Combine(txt_path.Text, i + item.Extension);
                if (item.Name.Length > 10)
                {
  //判斷是否重名
                    if (!File.Exists(destPath))
                    {
                        item.MoveTo(destPath);
                    } i
++; } } MessageBox.Show("操作完成"); //通過長寬新建文件夾 //將該尺寸的文件移動到文件夾下並重命名 } private void txt_path_Click(object sender, EventArgs e) { DialogResult dr = folderBrowserDialog1.ShowDialog(); if (dr == System.Windows.Forms.DialogResult.OK) { txt_path.Text = folderBrowserDialog1.SelectedPath; } }

 

界面:

 

附Demo下載地址


免責聲明!

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



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