c# - How can I check if the path entered by user is valid or not? -



c# - How can I check if the path entered by user is valid or not? -

this question has reply here:

check whether path valid 9 answers

i have found code while busy searching answer!

private void btnopenfile_click(object sender, eventargs e) { openfiledialog savefiledialogbrowse = new openfiledialog(); savefiledialogbrowse.filter = "pcap file|*.pcap"; savefiledialogbrowse.title = "save pcap file"; savefiledialogbrowse.showdialog(); var pcapfile = savefiledialogbrowse.filename; //do whatever selected filename if (pcapfile != "") { fileinfo fileinfo = new fileinfo(pcapfile); txtfilepath.text = fileinfo.fullname; } }

there no easy way.

you can utilize file.exists check file existence on path, alter can still happen before execution of next line. best alternative combine file.exists try-catch grab possible exceptions.

private void btnopenfile_click(object sender, eventargs e) { openfiledialog savefiledialogbrowse = new openfiledialog(); savefiledialogbrowse.filter = "pcap file|*.pcap"; savefiledialogbrowse.title = "save pcap file"; savefiledialogbrowse.showdialog(); var pcapfile = savefiledialogbrowse.filename; //do whatever selected filename seek { if (file.exists(pcapfile)) { fileinfo fileinfo = new fileinfo(pcapfile); txtfilepath.text = fileinfo.fullname; } } grab (filenotfoundexception filenotfoundexception) { //log , handle } grab (exception ex) { //log , handle } }

c#

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -