Automatically rename a file if it already exists in Windows way

前端 未结 10 646
谎友^
谎友^ 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:58

    int count= 0;
    

    file is the name of file

    while (File.Exists(fullpathwithfilename))  //this will check for existence of file
    { 
    // below line names new file from file.xls to file1.xls   
    fullpathwithfilename= fullpathwithfilename.Replace("file.xls", "file"+count+".xls"); 
    
    count++;
    }
    

提交回复
热议问题