我正在使用带有两个catch子句的try-catch块。我想从特定路径加载XML文件,并且正在检查目录中是否存在用于创建目录的第一个子句,以及检查目录中是否存在用于创建文件的第二个子句。但是,我知道如果目录不存在,文件也不会。
所以我在考虑是否有一种方法可以不重复代码。我知道我可以创建一个布尔变量,然后检查其是否为真并创建文件,但是我认为可能存在一个不错的,干净的解决方案,我只是不知道如何搜索。
XmlDocument document = new XmlDocument();
try
{
document.Load(folderPath + @"\XMLfile.xml"); // folderPath variable is assigned before depending on user input
}
catch(System.IO.DirectoryNotFoundException)
{
// if folder doesn't exist then the file will not either
System.IO.Directory.CreateDirectory(folderPath);
document.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
}
catch (System.IO.FileNotFoundException)
{
// if folder exists then the file might as well, if not, creating the file's structure
document.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
}
理想情况下,我想知道是否有一种避免重复代码但仍保留两个异常的方法。这两个catch子句中的布尔变量(例如createFile)是唯一以某种不错的方式进行操作的方法吗?
答案 0 :(得分:3)
怎么样
XmlDocument document = new XmlDocument();
try
{
document.Load(folderPath + @"\XMLfile.xml");
}
catch (Exception e) when (e is DirectoryNotFoundException || e is FileNotFoundException)
{
Directory.CreateDirectory(folderPath);
document.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
}
Directory.CreateDirectory(folderPath);
如果目录已经存在,则不执行任何操作。因此,在这里调用它是安全的。
答案 1 :(得分:1)
如果您使用的是C#6或更高版本,则可以使用when关键字过滤掉指定条件下的异常:
XmlDocument document = new XmlDocument();
try
{
document.Load(folderPath + @"\XMLfile.xml"); // folderPath variable is assigned before depending on user input
}
catch (Exception ex) when (ex is System.IO.DirectoryNotFoundException || ex is System.IO.FileNotFoundException)
{
// if folder doesn't exist then the file will not either
System.IO.Directory.CreateDirectory(folderPath);
document.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
}
但是,您没有完全相同的catch
块,因此您甚至可以考虑保留所有内容。否则,您需要在catch
块中引入另一个检查以调用CreateDirectory
。
答案 2 :(得分:0)
选中this thread。您将拥有基本的文件夹选择器,而不必尝试尝试捕获块来挠头。主要取决于您的应用程序。如果是服务器方面的内容,则应阅读有关Streams的内容(FileStream,StreamReader等。)here
答案 3 :(得分:0)
您正在使用异常来处理正常的控制流。您捕获的错误应无例外处理。
尽管如此,由于竞争条件,您仍然可以获得异常,因此您仍然需要异常处理。
另外,您应该考虑为格式错误的xml添加其他处理。
public static XmlDocument LoadOrCreateXmlDocument(string folderPath)
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string fileName = Path.Combine(folderPath + @"\XMLfile.xml");
if (TryLoadXmlDocument(fileName, out var document))
{
return document;
}
XmlDocument newDocument = new XmlDocument();
newDocument.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
return newDocument;
}
public static bool TryLoadXmlDocument(string fileName, out XmlDocument document)
{
document = new XmlDocument();
if (File.Exists(fileName))
{
try
{
document.Load(fileName);
return true;
}
catch (FileNotFoundException)
{
return false;
}
}
return false;
}
答案 4 :(得分:-1)
请尝试避免从catch中执行太多代码。这是解决更大问题的良方。 更好的方法是使用不同的异常来确定下一步该做什么,然后在方法结束时返回此结果。
因此,仅使用catch来处理异常,以后再进行下一步操作,而不要从catch内进行。
例如:
bool documentLoaded = LoadXML(); // Your code in this method
if (!documentLoaded)
CreateXML();
答案 5 :(得分:-1)
也许最好使用方法System.IO.Directory.Exists()
和System.IO.Directory.Exists()
而不是捕获异常。这样您的代码将更加清晰。
XmlDocument document = new XmlDocument();
if (!System.IO.Directory.Exists(folderPath))
System.IO.Directory.CreateDirectory(folderPath);
var filename = folderPath + @"\XMLfile.xml";
if (System.IO.File.Exists(filename);
document.Load(filename);
else
document.LoadXml("<?xml version=\"1.0\"?> \n" +
"<elements> \n" +
"</elements>");
}