c# - How to compare these two strings representing a file name -
i want compare 2 strings represent 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()) // << line { messagebox.show(name); } } }
i have question how that, comparing using 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 codes run search on file cache or directory on 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 set here if like... console.writeline(string.format("found: {0}", file)); } } /// <summary> /// finds matches file name in search text /// </summary> /// <param name="searchtext">the file path in 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 files referenced url , cache versions 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 file names both strings int lastindexofurl = url.lastindexof("/"); int lastindexofpath = filepath.lastindexof(@"\"); //move marker 1 ahead if placeholders 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; } }
Comments
Post a Comment