Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较这两个表示文件名的字符串_C#_Asp.net_File_Io - Fatal编程技术网

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

C# 如何比较这两个表示文件名的字符串,c#,asp.net,file,io,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";

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

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);
       }
   }
}
在哪里


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

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

您的问题不清楚,您是在试图比较两个字符串、两个文件名,还是在试图确定这两个路径是否引用了dame文件?请看一看:files1是一个字符串数组,无论您需要做什么比较,都应该与表示该数组中包含的单个文件名的name变量进行比较。
textbox1 = "http://localhost:11806/ourwork.html" 
files1   = "D:\M.Tech\Dissertation 2\Cache\ourwork.html"
    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;
    }
}