【问题标题】:Comparing file size with existing value将文件大小与现有值进行比较
【发布时间】:2015-07-13 08:46:20
【问题描述】:

我想在 foreach 循环的帮助下检查文件名及其关联的文件大小。所以我有以下 2 个包含文件名和文件大小的字符串数组:

string[] file_name = {
    "file1.dll",
    "file2.dll"
};
string[] file_size = {
    "17662", //file1.dll size
    "19019" //file2.dll size
};

通过下面的 foreach 循环,我正在检查文件和大小是否匹配

foreach (string filename in file_name)
        {
            foreach (string filesize in file_size)
            {
                if (File.Exists(Directory.GetCurrentDirectory() + "\\dll\\" + filename))
                {
                    FileInfo f = new FileInfo(Directory.GetCurrentDirectory() + "\\dll\\" + filename);
                    string s1 = f.Length.ToString();
                    if (s1 != filesize)
                    {
                        MessageBox.Show(f.Name + "modified DLL file, please change it to original");
                    }
                }
            }
        }

但它总是显示消息,我不知道错误在哪里。

【问题讨论】:

  • 问题很简单。您正在比较两个文件的大小......您可以创建一个 Dictionary (文件名作为键,大小作为值),然后遍历字典并将值与文件的大小进行比较。
  • 嘿@musium,感谢您的回答,但我是新手,您能给我示例链接,或编写示例/完整代码吗? :(
  • 为什么要使用两个单独的数组来表示文件大小和名称?
  • @M.NasserJavaid,有人提到我是新手,我需要帮助,而不是问题:(
  • @EvaldasL 我在我的回答中发布了一个使用字典的示例

标签: c# foreach filenames filesize


【解决方案1】:

建议你使用通用字典。

        //Create a dictionary that holds the file name with it's size
        Dictionary<string, long> FileNameAndSizes = new Dictionary<string, long>();

        //Key of dictionary will contain file name
        //Value of dictionary will contain file size
        FileNameAndSizes.Add("file1.dll", 17662);
        FileNameAndSizes.Add("file2.dll", 19019);

        //Iterate through the dictionary
        foreach (var item in FileNameAndSizes)
        {
            //Look for file existance
            if (File.Exists(Directory.GetCurrentDirectory() + "\\dll\\" + item.Key))
            {
                FileInfo f = new FileInfo(Directory.GetCurrentDirectory() + "\\dll\\" + item.Key);
                var s1 = f.Length;

                //Compare the current file size with stored size
                if (s1 != item.Value)
                {
                    MessageBox.Show(f.Name + "modified DLL file, please change it to original");
                }
            }
        }

【讨论】:

  • 我建议字典的类型应该是 Dictionary.
  • @AnupamSharma:感谢您的指出。编辑使如果长,与文件信息长度同步
【解决方案2】:

你可以让它变得简单

创建新类为 我的文件.cs

public class MyFiles
    {
        public string Name { get; set; }
        public long Size { get; set; }
        public string Path { get; set;}
    }
    public class MyWorkClass
    {
        private void InputFiles()
        {
            var files = new List<MyFiles>();
            var file = new MyFiles { Name = "File1.dll", Size = 1215454 };
            files.Add(file);
            file = new MyFiles { Name = "File2.dll", Size = 15544 };
            files.Add(file);
            ProcessFile(files);
        }

        private void ProcessFile(List<MyFiles> files)
        {
            foreach (MyFiles file in files)
            {
                var name = file.Name;
                var size = file.Size;
                //Do other things
            }
        }
    }

【讨论】:

  • 对于静态列表(我认为是),这需要相当多的额外代码。
  • @PMF 我给他举个例子,我不知道他是如何将数据放入数组中的。
【解决方案3】:

我不会为你写完整的代码..这是你的工作^^ 不过我会帮你的。

你必须这样做:

public void Test()
{
    //Use filename as key and file size as value
    var files = new Dictionary<String, Int32>
    {
        { "file1.dll", 17662 },
        { "file2.dll", 19019 }
    };

    foreach ( var file in files )
    {
        //..get filesize name is stored in file.Key
        if(fielsize != file.Value)
            //display error message
    }
}

1) 将文件名和文件大小存储在字典中,请参阅:for details

2) 使用您的代码获取每个文件的大小,并将大小与键值对的值进行比较。

【讨论】:

    【解决方案4】:

    你应该使用类似的东西

    Dictionary<string,int> files = new Dictionary<string, int>() { {"file1.dll", 12345}, {"file2.dll", 34566} };
    

    然后你可以只用一个循环进行迭代:

    foreach (var entry in files) // type of entry is "KeyValuePair<string,int>"
        {
            if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "dll",  entry.Key)))
            { 
                 // Compare file size against entry.Value here.
                 // ...
            }
        }
    }
    

    字典是为每个键存储一个值的数据结构。键和值都可以(几乎)是任何类型。

    此外:切勿使用 + 运算符连接路径。请改用 Path.Combine。

    【讨论】:

      【解决方案5】:

      您的问题是您将两个文件名与两个文件大小进行比较。

              foreach (var i in Enumerable.Range(0, file_name.Length))
              {
                  string filename = file_name[i];
                  string filesize = file_size[i];
      
                  if (File.Exists(Directory.GetCurrentDirectory() + "\\dll\\" + filename))
                  {
                      FileInfo f = new FileInfo(Directory.GetCurrentDirectory() + "\\dll\\" + filename);
                      string s1 = f.Length.ToString();
                      if (s1 != filesize)
                      {
                          MessageBox.Show(f.Name + "modified DLL file, please change it to original");
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2021-07-29
        • 1970-01-01
        • 2020-02-19
        • 2016-09-02
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多