【问题标题】:How to store values persistenly of files in a directory?如何将文件的值持久存储在目录中?
【发布时间】:2009-05-26 08:10:07
【问题描述】:

我正在使用 C# 在 VS2005 中开发一个 Windows 应用程序。在我的项目中,我生成 dll 并将它们存储在一个目录中。 dll 将被命名为 TestAssembly1、TestAssembly2、TestAssembly3 等。

所以考虑以上三个dll是否在目录中。下次用户使用我的项目时,我需要生成TestAssembly4、TestAssembly5等dll。

那么如何将 dll 的数量存储在文件夹中,并在下次使用项目时增加它们?

该目录甚至可以包含 dll 以外的文件。那么我该怎么做呢?

【问题讨论】:

    标签: c#


    【解决方案1】:

    我个人会使用二进制搜索来查找下一个程序集...

    • 开始 n=1
    • TestAssembly1.dll 是否存在? (是)
    • TestAssembly2.dll 是否存在? (是)
    • TestAssembly4.dll 是否存在? (是)
    • TestAssembly8.dll 是否存在? (是)
    • TestAssembly16.dll 是否存在? (是)
    • TestAssembly32.dll 是否存在? (不)

    并且不使用 16 到 32 之间的二分查找:

    • TestAssembly24.dll 是否存在? (是)
    • TestAssembly28.dll 是否存在? (是)
    • TestAssembly30.dll 是否存在? (不)
    • TestAssembly29.dll 是否存在? (是)

    所以使用 TestAssembly30.dll

    这避免了单独保留计数的需要,因此即使您删除所有文件也可以工作 - 二进制搜索意味着您的性能不会太差。

    未经测试,但如下所示;另请注意,基于文件存在的任何东西都会立即成为一种竞争条件(尽管通常是一个非常小的条件):

        static string GetNextFilename(string pattern) {
            string tmp = string.Format(pattern, 1);
            if (tmp == pattern) {
                throw new ArgumentException(
                     "The pattern must include an index place-holder", "pattern");
            }
            if (!File.Exists(tmp)) return tmp; // short-circuit if no matches
    
            int min = 1, max = 2; // min is inclusive, max is exclusive/untested
            while (File.Exists(string.Format(pattern, max))) {
                min = max;
                max *= 2;
            }
    
            while (max != min + 1) {
                int pivot = (max + min) / 2;
                if (File.Exists(string.Format(pattern, pivot))) {
                    min = pivot;
                }
                else {
                    max = pivot;
                }
            }
            return string.Format(pattern, max);
        }
    

    【讨论】:

    • 这取决于问题域。您需要对间隙进行特殊处理吗?在正常情况下,您是否希望目录中只存在少数文件。 (获取目录中的完整文件列表比相同数量的 file.exist 操作便宜得多)
    • @sambo99 - “获取完整列表....要便宜得多”;这取决于目录中的文件数...
    • @sambo99;请注意,只有 6 个文件。前 32 个文件存在测试。这种方法根本不需要您获取文件列表。
    • 测试大量文件的存在是否比枚举大量文件便宜?
    • @David - 你只测试可能存在的文件的 fraction - 所以上面对于密集目录是有效的。如果您有一个稀疏目录,那么很好:将它们全部列出并找到最大...
    【解决方案2】:

    您只需使用 Directory.GetFiles,为您要返回的文件传入一个模式:

    http://msdn.microsoft.com/en-us/library/wz42302f.aspx

    string[] files = Directory.GetFiles(@"C:\My Directory\", "TestAssembly*.dll");
    

    【讨论】:

      【解决方案3】:

      您可以获取所有程序集的列表,提取它们的 ID 并返回最高的 ID + 1,而不是检查文件是否已经存在:

      int nextId = GetNextIdFromFileNames(
                     "pathToAssemblies", 
                     "TestAssembly*.dll", 
                     @"TestAssembly(\d+)\.dll");
      
      [...]
      
      public int GetNextIdFromFileNames(string path, string filePattern, string regexPattern)
      {
          // get all the file names
          string[] files = Directory.GetFiles(path, filePattern, SearchOption.TopDirectoryOnly);
      
          // extract the ID from every file, get the highest ID and return it + 1
          return ExtractIdsFromFileList(files, regexPattern)
                 .Max() + 1;
      }
      
      private IEnumerable<int> ExtractIdsFromFileList(string[] files, string regexPattern)
      {
          Regex regex = new Regex(regexPattern, RegexOptions.IgnoreCase);
      
          foreach (string file in files)
          {
              Match match = regex.Match(file);
              if (match.Success)
              {
                  int value;
                  if (int.TryParse(match.Groups[1].Value, out value))
                  {
                      yield return value;
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-10
        • 2018-08-25
        • 2012-11-15
        • 1970-01-01
        • 2013-05-09
        • 2012-08-13
        • 2020-01-18
        • 1970-01-01
        • 2010-11-29
        相关资源
        最近更新 更多