文件和文件夹重命名---递归思想

来源:互联网 发布:ios斗地主源码 编辑:程序博客网 时间:2024/05/24 05:22

方法:文件夹( Directory.Move(strFullname, strUpFullName + "//" + docCount.ToString());)

方法:文件fi.MoveTo(Path.Combine(dir.FullName, i.ToString() +this.textBoxString.Text.Trim()+ fi.Extension));//重命名   要将文件移动的路径 可将文件重命名

    (Flile。move())

 

 //重命名的方法
        int docCount = 0;
        void Rename(DirectoryInfo dir)
        {
            int i = 0;
            if (dir.Exists)//指定文件的目录是否存在
            {  
                foreach (FileInfo fi in dir.GetFiles())//获取当前目录的文件列表        文件夹不属于文件列表
                {
                   
                    if (fi.Extension.ToLower()==".jpg")
                    {
                        //将现有信息写入数据库
                        string strPath = "";//设置缺省值
                        strPath = fi.FullName;
                        strPath = strPath.Replace("//", "//");//替换符号
                        strPath = strPath.Substring(2);
                        strPath = "~" + strPath;//写死路径也行
                        string strYear = "";
                        strYear = dir.Parent.ToString();
                        string strMonth = "";
                        strMonth = dir.Name;
                        string strContant = "";
                        strContant = fi.Name.Remove(fi.Name.Length - 4, 4);

                        fi.MoveTo(Path.Combine(dir.FullName, i.ToString() +this.textBoxString.Text.Trim()+ fi.Extension));//重命名   要将文件移动的路径 可将文件重命名
                        //应该记录改名以后的路径
                        string strSQL = string.Format("insert into tb_goodnews (path,year,month,contant) values ('{0}','{1}','{2}','{3}')", strPath, strYear, strMonth, strContant);
                        DBServer.DBServerSQL.GetExecuteNonQueryBool(strSQL);
                        i++;
                    }
                }
                foreach (DirectoryInfo di in dir.GetDirectories())//子目录数组
                {
                    docCount++;
                    string strFullname = di.FullName;
                    int index = strFullname.LastIndexOf("//");
                   string strUpFullName = strFullname.Substring(0, index);
                   //File.Move(strFullname,strUpFullName+"//"+docCount.ToString());//旧 新
                   Directory.Move(strFullname, strUpFullName + "//" + docCount.ToString());
                    Rename(di);
                 }
            }
        }

 

原创粉丝点击