如何比较表示文件名的这两个字符串

时间:2015-04-01 15:56:49

标签: c# asp.net file io

我想比较两个代表文件名的字符串:

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"

1 个答案:

答案 0 :(得分:0)

它汇总了一些代码,可以帮助您在计算机上的任何文件缓存或目录上运行此搜索。

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