c# - How do I check if one of the lines already exist in the text file? -
string filename = ""; private void opentoolstripmenuitem1_click(object sender, eventargs e) { openfiledialog thedialog = new openfiledialog(); thedialog.title = "open text file"; thedialog.filter = "txt files|*.txt"; thedialog.initialdirectory = @"c:\"; if (thedialog.showdialog() == dialogresult.ok) { lines = file.readalllines(recentfiles); filename = thedialog.filename; (int = 0; < lines.length; i++) { recentfiles = new streamwriter(recentfiles, true); recentfiles.writeline(thedialog.filename); recentfiles.close(); items = file .readlines(recentfiles) .select(line => new toolstripmenuitem() { text = line }) .toarray(); recentfilestoolstripmenuitem.dropdownitems.addrange(items); } textfilecontenttorichtextbox(filename); } }
i'm not sure if doing loop on lines right. want when open new text file check if exists in recentfiles(recentfiles.txt)
, if exist don't write again.
loop on lines , if after looping on lines , variable filename not exist write text file, , update items.
linq perfect this:
// if there not lines equal filename if (!lines.any(line => line.equals(filename))) { // write recent files text file }
Comments
Post a Comment