我遇到了一些麻烦StreamReader
,我有一个设置文件,我在其中保存设置。我想以一种我也可以处理异常的方式打开和关闭文件。
当文件无法加载时,我想暂时返回 false。
我创建了一个为我加载文件的函数:
private bool LoadSettingsFile(out StreamReader SettingsFile)
{
try
{
SettingsFile = new StreamReader("Settings.txt");
return true;
}
catch
{
//Going to solve the exception later, but if I can't I want to return false.
SettingsFile = new StreamReader(); //You need to assign StreamReader, but you need to open a file for that.
//'System.IO.StreamReader' does not contain a constructor that takes 0 arguments
return false;
}
}
我以这种方式调用该函数:
StreamReader SettingsFile;
if (!LoadSettingsFile(out SettingsFile))
return false;
我怎样才能避免或解决这个问题?