C# 多个文件,同名

C# 多个文件,同名,c#,scripting,copy,C#,Scripting,Copy,我正在尝试保存几个文件,它们都有相同的名称。我想在名称执行以下操作时执行以下操作:file.extension文件[1]。extension文件[2]。extension我尝试过这个方法,但对我无效 下面是一些代码(不是全部,只是相关部分) 这太离谱了。此外,如果“.extension”出现在文件名的某个地方而不是末尾,则会中断(因此,使字符串处理比示例代码更智能)。如果需要,可以使用Path.GetExtension(Path) 您可能需要使用Path.GetExtension(string

我正在尝试保存几个文件,它们都有相同的名称。我想在名称执行以下操作时执行以下操作:file.extension文件[1]。extension文件[2]。extension我尝试过这个方法,但对我无效

下面是一些代码(不是全部,只是相关部分)


这太离谱了。此外,如果“.extension”出现在文件名的某个地方而不是末尾,则会中断(因此,使字符串处理比示例代码更智能)。如果需要,可以使用
Path.GetExtension(Path)


您可能需要使用
Path.GetExtension(string Path)
来获取文件的扩展名,然后只需剪切文件名并在其上附加迭代号,然后再添加扩展名。

您不能将具有相同名称和扩展名的文件放在同一文件夹中,因此您有如下选项:

  • 将它们全部放在具有有意义的名称的seprate目录中(即使文件具有相同的名称,内容也应该是不同的)

  • 在前面使用日期时间戳,如在每个文件前面使用YYYYMMDD(但它违反了“相同文件名”规则)

  • 这同样适用于扩展


    • 我忍不住觉得这些年来我做了太多次了。:)

      试着做以下几点:

              List<string> filesToSave = new List<string>();
              var fileName = "myfile.ext";
              var baseName = Path.GetFileNameWithoutExtension(fileName);
              var extension = Path.GetExtension(fileName);
      
              int counter = 1;
              foreach (var fileToSave in filesToSave)
              {
                  var tempFileName = fileName;
                  while (File.Exists(tempFileName))
                  {
                      tempFileName = string.Format("{0}{1}.{2}", baseName, counter, extension);
                      counter++
                  }
      
                  File.WriteAllText(tempFileName, fileToSave);
              }
      
      List filesToSave=new List();
      var fileName=“myfile.ext”;
      var baseName=Path.GetFileNameWithoutExtension(文件名);
      var extension=Path.GetExtension(文件名);
      int计数器=1;
      foreach(变量fileToSave in filesToSave)
      {
      var tempFileName=文件名;
      while(File.Exists(tempFileName))
      {
      tempFileName=string.Format(“{0}{1}.{2}”,baseName,counter,extension);
      柜台++
      }
      writealText(临时文件名,fileToSave);
      }
      
      以下是一个如何帮助自己的示例:

              string[] files = { "file.txt", "file.txt", "file.txt" };
              for(int i=0;i<files.Length;i++)
              {
                  string fileName = Path.GetFileNameWithoutExtension(files[i]);
                  string extension = Path.GetExtension(files[i]);
                  fileName = fileName + (i + 1);
                  //if you would like to aqdd square brackets, you can change upper line to:
                  fileName = string.Format("{0}{1}{2}{3}", fileName, "[", (i + 1), "]");
                  files[i] = String.Concat(fileName, extension);
              }
      
      string[]files={“file.txt”、“file.txt”、“file.txt”};
      对于(int i=0;i插入“sPathAndFileName”,并使用输出的“sSaveName”保存文件。
      您将看到友好的递增文件名,如:Test.txt、Text[1].txt、Test[2].txt等

          int index = 0;
          string sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                           + Path.GetFileNameWithoutExtension(sPathAndFileName)
                           + Path.GetExtension(sPathAndFileName);
          while (File.Exists(sSaveName) == true)
          {
              sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                        + Path.GetFileNameWithoutExtension(sPathAndFileName)
                        + "[" + (++index).ToString() + "]"
                        + Path.GetExtension(sPathAndFileName);
          }
      
      试试这个。 简单得多

      string filename = "C:\bla.txt";
      string filenameNoPath = Path.GetFileNameWithoutExtension(filename);
      string temppath = Path.GetDirectoryName(filename);
      string extension = Path.GetExtension(filename);
      
      if (!File.Exists(path))
      {
           File.WriteAllBytes(filename, file);
      }
      else
      {
           do
           {
                 counter++; // we're here, so lets count files with same name
                 string path = temppath + "\\" + filenameNoPath + "(" + counter.ToString() + ")" + extension;
           } while (File.Exists(path));
      
           File.WriteAllBytes(path, file);
       }
      

      我没有任何代码可以共享,主要是因为它充满了异常和垃圾,这让我很生气。那么你的问题到底是什么?我想保存这些文件,而不丢失它们,每个文件夹中大约有400个不同的文件夹,每个文件夹中大约有20个不同的文件,所以我不能手动执行。你在哪里有这些文件?在一些数组中,李ke FileInfo[]?我尝试的每件事都会让它吐出毫无意义的错误,就像在脚本末尾我有一个}它说我需要一个,;,我会尝试一下,这是为了填充至少15个字符。我完全忘记了为什么我会这样做(可能是为了在解析坏文件名时故意出错),但是您可以通过将sSaveName声明更改为以下内容使代码更加简单:string sSaveName=sPathAndFileName;
          int index = 0;
          string sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                           + Path.GetFileNameWithoutExtension(sPathAndFileName)
                           + Path.GetExtension(sPathAndFileName);
          while (File.Exists(sSaveName) == true)
          {
              sSaveName = Path.GetDirectoryName(sPathAndFileName) + @"\"
                        + Path.GetFileNameWithoutExtension(sPathAndFileName)
                        + "[" + (++index).ToString() + "]"
                        + Path.GetExtension(sPathAndFileName);
          }
      
      string filename = "C:\bla.txt";
      string filenameNoPath = Path.GetFileNameWithoutExtension(filename);
      string temppath = Path.GetDirectoryName(filename);
      string extension = Path.GetExtension(filename);
      
      if (!File.Exists(path))
      {
           File.WriteAllBytes(filename, file);
      }
      else
      {
           do
           {
                 counter++; // we're here, so lets count files with same name
                 string path = temppath + "\\" + filenameNoPath + "(" + counter.ToString() + ")" + extension;
           } while (File.Exists(path));
      
           File.WriteAllBytes(path, file);
       }