将文件大小与现有值进行比较

时间:2015-07-13 08:46:21

标签: c# foreach filenames filesize

我想在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");
                    }
                }
            }
        }

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

5 个答案:

答案 0 :(得分: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");
                }
            }
        }

答案 1 :(得分:0)

您可以将其简化为

创建新课程 MyFiles.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
            }
        }
    }

答案 2 :(得分:0)

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

你必须做这样的事情:

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)使用您的代码获取每个文件的大小,并将大小与键值对的值进行比较。

答案 3 :(得分:0)

您应该使用类似

的内容
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。

答案 4 :(得分:0)

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

        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");
                }
            }
        }