文件夹改名

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiajing13579/article/details/83067768

1:选择需要改名的目录:

string defaultPath = "";
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            //打开的文件夹浏览对话框上的描述
            dialog.Description = "请选择一个文件夹";
            //是否显示对话框左下角 新建文件夹 按钮,默认为 true
            dialog.ShowNewFolderButton = false;
            //首次defaultPath为空,按FolderBrowserDialog默认设置(即桌面)选择
            if (defaultPath != "")
            {
                //设置此次默认目录为上一次选中目录
                dialog.SelectedPath = defaultPath;
            }
            //按下确定选择的按钮
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //记录选中的目录
                defaultPath = dialog.SelectedPath;
            }

            this.textBox1.Text = defaultPath;

2. 修改目录下所有的文件夹的名称

string path = this.textBox1.Text;
            DirectoryInfo root = new DirectoryInfo(path);
            DirectoryInfo[] dics = root.GetDirectories();
           
            int wei = Int32.Parse(this.textBox3.Text); // 后面加数字的位数
            for (int i = 0; i < dics.Length; i++)
            {

              // 目录 + 前缀名+ 数字 ,数字的位数用 wei 设置

                string destPath = path + "//" + textBox2.Text + (i + 1).ToString().PadLeft(wei, '0');
                if (!File.Exists(destPath)){
                    dics[i].MoveTo(destPath);
                }
               
            }
            MessageBox.Show("完成");

猜你喜欢

转载自blog.csdn.net/xiajing13579/article/details/83067768