【问题标题】:Determine file pairs in a directory, same name but different extensions确定目录中的文件对,同名但扩展名不同
【发布时间】:2014-10-26 13:34:43
【问题描述】:

我目前正在开发一个 .NET 2.0 应用程序,但在尝试遍历特定文件夹中的所有文件并确定其文件对时遇到了问题。 (同名但扩展名不同的文件)

喜欢:

  • MyFile0001.jpg
  • MyFile0001.mp3
  • MyFile0001.txt

我对文件扩展名感兴趣,无论它们是否可用,这就是我创建一个名为“FileClass”的类的原因。最后,文件对将添加到列表list.AddRange()

她是我的代码示例:

class FileClass
{
    public string FileName { get; set; }
    public string filePath { get; set; }
    public bool mp3 { get; set; }
    public bool txt { get; set; }
    public bool jpg { get; set; }
}

static void Main()
{
    var list = new List<FileClass>();

    var dirConfig = new DirectoryInfo(@"New Folder");
    var allFiles = dirConfig.GetFiles("*");

    foreach (var fileInfo in allFiles)
    {
        // Some code for finding file pairs...
        // set properties, if filename extension is available
        // and add them to a list. 
    }
}

任何建议如何处理?
如果可能没有 LINQ

【问题讨论】:

    标签: c# .net-2.0


    【解决方案1】:

    我会使用Dictionary&lt;String, List&lt;String&gt;&gt;(用于文件名 -> 扩展名列表)
    第一步可能如下所示:

    Dictionary<String, List<String>> d = new Dictionary<string, List<string>>();
    var dirConfig = new DirectoryInfo(@"SomeFolder");
    var allFiles = dirConfig.GetFiles("*");
    foreach (var fileInfo in allFiles)
    {
         String coreName = Path.GetFileNameWithoutExtension(fileInfo.Name);
         if (!d.ContainsKey(coreName)) d.Add(coreName, new List<String>());
         d[coreName].Add(fileInfo.Extension);
    }
    

    【讨论】:

      【解决方案2】:

      这样的事情应该可以工作:

      class FileClass
      {
          public string FileName { get; set; }
          public string filePath { get; set; }
          public bool mp3 { get; set; }
          public bool txt { get; set; }
          public bool jpg { get; set; }
      }
      
      static void Main()
      {
          var list = new List<FileClass>();
      
          var dirConfig = new DirectoryInfo(@"New Folder");
          var allFiles = dirConfig.GetFiles("*");
      
          foreach (var fileInfo in allFiles)
          {
              // for purposes of matching files with different extensions,
              // the "fileName" variable below is the file name minus the extension
              var fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length
                  - fileInfo.Extension.Length);
              var fileClass = list.Where(fc => fc.FileName == fileName).FirstOrDefault();
      
              if (fileClass == null)
              {
                  // this is the first version of this file name,
                  // so create a new FileClass instance and add it to the list
                  fileClass = new FileClass({
                      FileName = fileName,
                      filePath = Path.Combine(fileInfo.DirectoryName, fileName),
                      mp3 = (fileInfo.Extension.ToLower() == ".mp3"),
                      txt = (fileInfo.Extension.ToLower() == ".txt"),
                      jpg = (fileInfo.Extension.ToLower() == ".jpg")
                  });
                  list.Add(fileClass);
              }
              else
              {
                  // a different version of this file was already found,
                  // so just set the boolean flag for this extension
                  switch (fileInfo.Extension.ToLower()) {
                      case ".mp3": fileClass.mp3 = true; break;
                      case ".txt": fileClass.txt = true; break;
                      case ".jpg": fileClass.jpg = true; break;
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以这样做。

        string[] arrayOfFiles= Directory.GetFiles(folderPath, "Filename.*");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-24
          • 1970-01-01
          相关资源
          最近更新 更多