I want to check filename and its associated filesize with the help of a foreach loop. So I have the below 2 string arrays containing file name and file size:
string[] file_name = {
"file1.dll",
"file2.dll"
};
string[] file_size = {
"17662", //file1.dll size
"19019" //file2.dll size
};
And with below foreach loops I am checking the file and size are matching
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");
}
}
}
}
But it always shows the message, I don't know where is the error.
Suggest you to use generic dictionary.
//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");
}
}
}