c# - Catch "FileNotFoundException" -
i have method folder path of particular file:
string filepath = path.combine(environment.getfolderpath( environment.specialfolder.mydocuments), "file.txt");
and later, use read text in file:
streamreader rdr = new streamreader(filepath); // "c:\users\<user>\documents\file.txt" string mystring = rdr.readtoend();
trouble is, if file doesn't exist, throws filenotfoundexception
(obviously). want use if/else
catch error, in user can browse find file directly, i'm not sure use verify if filepath
valid or not.
for example, can't use:
if (filepath == null)
because top method retrieve string return value, whether or not valid. how can solve this?
you can use file.exists:-
if(file.exists(filepath)) { //do } else { }
Comments
Post a Comment