[英]How to compare these two strings representing a file name
I want to compare two strings that represent a file name: 我想比较两个代表文件名的字符串:
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);
}
}
}
I have a question about how to do that, I am now comparing using this line: 我有一个关于如何执行此操作的问题,我现在使用以下代码进行比较:
if (textBox1.Text.Substring(23,30) == files1.ToString())
Where 哪里
textbox1 = "http://localhost:11806/ourwork.html"
files1 = "D:\M.Tech\Dissertation 2\Cache\ourwork.html"
It have put together some codes that will help you run this search on any file cache or directory on your machine. 它汇总了一些代码,可帮助您在计算机上的任何文件缓存或目录上运行此搜索。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.