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 answersi 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
Post a Comment