我的项目中有一个"Docs“文件夹。我想要创建并写入该文件夹下的XML文件。以下是我的尝试:
string myPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var finalPath = Path.Combine(myPath, "Docs");
using (XmlWriter writer = XmlWriter.Create(finalPath + @"\employees.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Employees");...我明白这一例外:
{“找不到路径'C:\Users...\ConsoleApplication6\ConsoleApplication6\bin\Debug\Docs\employees.xml'."}的一部分”
我怎么才能解决这个问题?
发布于 2014-09-25 07:51:04
项目结构中的目录是(谢天谢地!)没有复制到项目的输出-因此,当您试图访问Assembly.GetExecutingAssembly().Location下的子目录(正如您可能看到的那样,默认情况下= YOUR_PROJECT_PATH + \bin\ +Debug发行版)时,您将无法找到它。
Directory.CreateDirectory是您的朋友;额外的好处是,如果目录已经存在,这不会造成任何(不抛出任何异常),因此可以安全地使用它来确保给定目录的存在。
https://stackoverflow.com/questions/26033182
复制相似问题