2015-07-13 77 views
0

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

但它始終顯示消息,我不知道錯誤在哪裏。

+0

問題很簡單。您正在比較兩個文件的大小...... 您可以創建一個Dictionary (Filename as key,Size as value),然後遍歷字典並將該值與文件大小進行比較。 – musium

+0

Heh @musium,謝謝你的回答,但我是新手,你能舉個例子鏈接,還是寫例子/完整的代碼? :( – EvaldasL

+1

爲什麼你使用兩個單獨的陣列文件大小和名稱? –

回答

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

我會建議字典應該是字典類型

+0

@AnupamSharma:感謝您指出。編輯如果長,與文件信息長度同步 – Yogi

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 
      } 
     } 
    } 
+0

對於一個靜態列表(我假設它是這樣),這需要相當多的額外代碼。 – PMF

+0

@PMF我給他一個例子,我不知道他是如何將數據放入數組 –

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)使用你的代碼獲取每個文件的大小和比較大小與鍵值對的值。

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。

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