将文件大小与现有值进行比较
Comparing file size with existing value
我想借助 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");
}
}
}
}
但是一直显示这个信息,不知道哪里出错了
你可以把它简化为
创建新的 class 作为
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
}
}
}
建议您使用通用词典。
//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");
}
}
}
我不会为你写完整的代码..这是你的工作^^
不过我会帮你的。
你必须这样做:
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) 使用您的代码获取每个文件的大小,并将大小与键值对的值进行比较。
你应该使用像
这样的东西
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。
你的问题是你正在比较两个文件名和两个文件大小。
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");
}
}
}
我想借助 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");
}
}
}
}
但是一直显示这个信息,不知道哪里出错了
你可以把它简化为
创建新的 class 作为 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
}
}
}
建议您使用通用词典。
//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");
}
}
}
我不会为你写完整的代码..这是你的工作^^ 不过我会帮你的。
你必须这样做:
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) 使用您的代码获取每个文件的大小,并将大小与键值对的值进行比较。
你应该使用像
这样的东西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。
你的问题是你正在比较两个文件名和两个文件大小。
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");
}
}
}