Automatically rename a file if it already exists in Windows way

前端 未结 10 636
谎友^
谎友^ 2020-11-28 06:50

My C# code is generating several text files based on input and saving those in a folder. Also, I am assuming that the name of the text file will be same as input.(The input

10条回答
  •  攒了一身酷
    2020-11-28 06:56

    It's working fine now. thanks guys for the answers..

    string[] allFiles = Directory.GetFiles(folderPath).Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray();
            string tempFileName = fileName;
            int count = 1;
            while (allFiles.Contains(tempFileName ))
            {
                tempFileName = String.Format("{0} ({1})", fileName, count++); 
            }
    
            output = Path.Combine(folderPath, tempFileName );
            string fullPath=output + ".xml";
    

提交回复
热议问题