判斷一個文件在該文件夾下是否存在,若存在則在名稱后加_在累加1

来源:互联网 发布:南京军区总院网络挂号 编辑:程序博客网 时间:2024/06/05 00:40

 

//判斷一個文件在該文件夾下是否存在,若存在則在名稱后加_在累加1

public string ExistsFiles(string filePath)
        {
            if (File.Exists(filePath.ToString()))
            {
                FileInfo myFile = new FileInfo(filePath.ToString());
                if (myFile.Name != s_OldFileName)
                {
                  
                    string temp=myFile.Name.Substring(s_OldFileName.Length - 4, myFile.Name.Length - (s_OldFileName.Length - 4));
                   
                    strList = temp.Split('.');
                    filePath = myFile.DirectoryName + "\\" + s_OldFileName.Replace(".doc", "") + "_" + (int.Parse(strList[0].Replace("_", "")) + 1) + ".doc";
                   
                   
                }
                else
                {
                    filePath = myFile.DirectoryName + "\\" + myFile.Name.Replace(myFile.Extension, "") + "_1.doc";
                }
                return  ExistsFiles(filePath);
            }
            return filePath;

        }

0 0