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. maybe file3.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

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -