gpt4 book ai didi

c# - 找不到路径的一部分?

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:15 26 4
gpt4 key购买 nike

我有以下代码:

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();
}
}

文件路径和名称将为 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

最佳答案

我试过代码,ArgumentExceptionXmlTextWriter 构造函数抛出并显示以下消息:

the URI formats are not supported.

考虑以下代码:

// Get the path to assembly directory.
// There is a lot of alternatives: http://stackoverflow.com/questions/52797/
var assemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
var directoryPath = Path.GetDirectoryName(assemblyPath);

// Path to XML-file.
var filePath = Path.Combine(directoryPath, "ExceptionLog.xml");

using (var xmlTextWriter = new XmlTextWriter(filePath, Encoding.UTF8))
{
...
}

关于c# - 找不到路径的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com