我有以下代码:
fileinfo = new FileInfo(filePathAndName);
if (!fileinfo.Exists)
{
using (xmlWriter = new XmlTextWriter(filePathAndName, System.Text.Encoding.UTF8))
{
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root");
xmlWriter.WriteStartElement("objects");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
}
filePathAndName 将是C:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.
该文件夹确实存在,但该文件不存在。在这种情况下,XmlTextWriter 应该创建文件,但它会抛出Could not find part of the path
.
这可能是我在这里忘记的非常明显的事情,请帮忙。
编辑:这就是路径的真实样子:
C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin
这就是代码中使用的 URL 的生成方式:
(new System.Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "\\ExceptionLog.xml")).AbsolutePath