c# - Compare paths in texboxes -
c# - Compare paths in texboxes -
i have couple of textboxes in winform. insert paths files in these textboxes - example:
textbox1: c:\users\file1.txt textbox2: c:\users\john\desktop\file2.txt textbox3: c:\file1.txt textbox4: d:\stuff\file3.txt . . . i'm trying check:
if each file exists (e.g. maybefile3.txt not exist in case). if of files have same name (e.g. file1.txt in case). how can to the lowest degree amount of code? code way way long kind of thing.
this came checking if files exist. , have no thought how start dealing duplicated file.
int filesdontexist = 0; if (!string.isnullorempty(textbox1.text)) { if (!file.exists(textbox1.text)) { filesdontexist++; } } if (!string.isnullorempty(textbox2.text)) { if (!file.exists(textbox2.text)) { filesdontexist++; } } if (!string.isnullorempty(textbox3.text)) { if (!file.exists(textbox3.text)) { filesdontexist++; } } if (!string.isnullorempty(textbox4.text)) { if (!file.exists(textbox4.text)) { filesdontexist++; } } if (filesdontexist == 0) { messagebox.show("all files exist!"); } else { messagebox.show("at to the lowest degree 1 file doesn't exist!"); }
first paths using linq's select method:
var paths = this.controls.oftype<textbox>().select(x => x.text); then utilize file.exists determine whether files exists:
var allfilesexist = paths.all(file.exists); and file names using path.getfilename method check if there duplicates using groupby:
var isduplicate = paths.select(x => path.getfilename(x)) .groupby(x => x) .any(g => g.count() > 1); c# visual-studio-2012 textbox
Comments
Post a Comment