2015-04-01 42 views
0

我想比較兩個字符串表示文件名這兩個字符串比較:如何表示文件名

private void button2_Click(object sender, EventArgs e) 
{ 
    string search = textBox1.Text; 
    string[] files = Directory.GetFiles(@"C:\Cache", "*.*", SearchOption.AllDirectories); 
    int Flag = 0; 
    string dir = @"C:\Cache"; 
    string[] files1; 
    int numFiles; 
    files1 = Directory.GetFiles(dir); 
    numFiles = files.Length; 

    MessageBox.Show("Files searched : " + numFiles); 
    Console.WriteLine("Files searched : " + numFiles + "<br>"); 
    foreach (string name in files1) 
    { 
     if (textBox1.Text.Substring(23,30) == files1.ToString()) // << this line 
     { 
      MessageBox.Show(name); 
     } 
    } 
} 

我有一個關於如何做到這一點,我現在用的這條線比較的問題:

if (textBox1.Text.Substring(23,30) == files1.ToString()) 

textbox1 = "http://localhost:11806/ourwork.html" 
files1 = "D:\M.Tech\Dissertation 2\Cache\ourwork.html" 
+0

你的問題不清楚,你想比較兩個字符串,兩個文件名或你想確定兩個路徑是否指向dame文件?請看看:http://stackoverflow.com/help/how-to-ask – Alex 2015-04-01 17:22:55

+0

'files1'是一個字符串數組,無論您需要做什麼比較,都應該與代表單個文件的'name'變量該數組中包含的名稱。 – Alex 2015-04-01 17:35:22

回答

0

它已經把一些代碼,這將有助於您運行的任何文件高速緩存或可怕的這個搜索您的機器上的ctory。

private void button2_Click(object sender, EventArgs e) { 
    { 
     List<string> searchResults = SearchCache(textBox1.Text, @"C:\Cache"); 

     foreach (string file in searchResults) 
     { 
      //MessageBox popup can be set up here if you like... 
      Console.WriteLine(String.Format("Found: {0}", file)); 
     } 
    } 

    /// <summary> 
    /// Finds all matches to the file name in the search text 
    /// </summary> 
    /// <param name="searchText">The file path in the search text</param> 
    /// <param name="cachePath">The cache path</param> 
    /// <returns></returns> 
    private List<string> SearchCache(string searchText, string cachePath) 
    { 
     string[] files = Directory.GetFiles(cachePath, "*.*", SearchOption.AllDirectories); 

     Console.WriteLine(String.Format("No. of files in cache: {0}", files.Length)); 

     List<string> searchResults = new List<string>(); 

     foreach (string file in files) 
      if (AreFileReferencesSame(searchText, file)) 
       searchResults.Add(file); 

     Console.WriteLine(String.Format("No. of matches: {0}", searchResults.Count)); 

     return searchResults; 
    } 

    /// <summary> 
    /// Checks if the files referenced by a URL and the cache versions are the same 
    /// </summary> 
    /// <param name="url">Url path</param> 
    /// <param name="filePath">Cached file full path</param> 
    /// <returns></returns> 
    private bool AreFileReferencesSame(string url, string filePath) 
    { 
     //Extract the file names from both strings 
     int lastIndexOfUrl = url.LastIndexOf("/"); 
     int lastIndexOfPath = filePath.LastIndexOf(@"\"); 

     //Move the marker one ahead if the placeholders are found 
     lastIndexOfUrl = lastIndexOfUrl >= 0 ? lastIndexOfUrl + 1 : 0; 
     lastIndexOfPath = lastIndexOfPath >= 0 ? lastIndexOfPath + 1 : 0; 

     string urlFilename = url.Substring(lastIndexOfUrl).Trim(); 
     string diskFilename = filePath.Substring(lastIndexOfPath).ToString(); 

     if (urlFilename.Equals(diskFilename, StringComparison.CurrentCultureIgnoreCase)) 
      return true; 
     else 
      return false; 
    } 
} 
相關問題