C# 找不到路径的一部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14584715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find a part of the path?
提问by Banshee
I have the following code :
我有以下代码:
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();
}
}
The filePathAndName will be C:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.
filePathAndName 将是 C:/MyApp%205/Produkter/MyApp%20Utveckling/Host/Orbit.Host.Dev/bin/ExceptionLog.xml.
The folder does exists but the file does not. XmlTextWriter should in this case create the file but instead it throws Could not find part of the path
.
文件夹确实存在,但文件不存在。在这种情况下,XmlTextWriter 应该创建文件,但它会抛出Could not find part of the path
.
It's probably something very obvious I have forgotten here, please help.
这可能是我在这里忘记的非常明显的事情,请帮忙。
Edit : This is how the path really looks like :
编辑:这是路径真正的样子:
C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin
And this is how the URL that is used in the code is generated :
这就是代码中使用的 URL 的生成方式:
(new System.Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "\ExceptionLog.xml")).AbsolutePath
采纳答案by Sergey Brunov
I've tried the code, ArgumentException
is thrown by XmlTextWriter
constructor with this message:
我已经尝试过代码,ArgumentException
是由XmlTextWriter
构造函数抛出的,带有以下消息:
the URI formats are not supported.
不支持 URI 格式。
Consider the following code:
考虑以下代码:
// 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))
{
...
}
回答by Naga Sandeep
Try this -- add @ before the filePathAndName
试试这个——在 filePathAndName 之前添加 @
string filePathAndName = @"C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin\text.xml";
FileInfo fileinfo = new FileInfo(filePathAndName);
if (!fileinfo.Exists)
{
using (XmlTextWriter 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();
}
}
回答by Oliver
Instead of using Uri.AbsolutePath
you should take Path.Combine()
而不是使用Uri.AbsolutePath
你应该采取Path.Combine()
var filepath = @"C:\MyApp 5\Produkter\MyApp Utveckling\Host\Orbit.Host.Dev\Bin"
var filename = Path.Combine(filepath, "ExceptionLog.xml");
var fileInfo = new FileInfo(filename);
if(!fileInfo.Exists)
{
//ToDo: call xml writer...
}
回答by RogerN
Use Assembly.Location and Path.Combine to form your fileNameAndPath variable:
使用 Assembly.Location 和 Path.Combine 来形成你的 fileNameAndPath 变量:
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePathAndName = Path.Combine(folder, "ExceptionLog.xml");
回答by MacGyver
If you are interacting with a path on a network (aka UNC path), you have to use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you're opening files, creating, updating and deleting files, opening directories and deleting directories on a network path, use Server.MapPath
.
如果您正在与网络上的路径(又名 UNC 路径)交互,则必须使用 Server.MapPath 将 UNC 路径或虚拟路径转换为 .NET 可以理解的物理路径。因此,无论何时打开文件、创建、更新和删除文件、打开目录和删除网络路径上的目录,都可以使用Server.MapPath
.
Example:
例子:
System.IO.Directory.CreateDirectory(Server.MapPath("\server\path"));