c# - Static string using File.ReadAllText -
i attempting use file.readalltext
static readonly string. issue compiling .netz , when program starts looks string , if not found nasty exception. possible around using if/else somehow?
usually have popup messagebox or in label stating file not found.using .netz if file not found error message , program not start.
this static string used throughout program
static readonly string config = file.readalltext("config.ini");
i using .netz include dll.
rather doing inline declaration, move work static constructor. becomes trivial add try/catch
blocks handle error cases see fit:
public class myclass { static readonly string config; static myclass() { try { config = file.readalltext("config.ini"); } catch (filenotfoundexception) { //do else //use default configuration? //report user? //crash , burn? } } }
note there many reasons why reading file may fail besides not being found @ path. may want consider catching of well.
Comments
Post a Comment