简体   繁体   English

DirectoryNotFoundException未通过用户代码处理? (Visual Studio 2013,C#)

[英]DirectoryNotFoundException was unhandled by user code? (Visual Studio 2013, C#)

When I try to run this program, which writes details about a book to an xml file when a button is clicked, I get a DirectoryNotFoundException. 当我尝试运行该程序时,当单击按钮时,该程序将有关书籍的详细信息写入xml文件中,因此出现DirectoryNotFoundException。 How can I rewrite the address for my books.xml file under the App_Data folder? 如何重写App_Data文件夹下我的books.xml文件的地址?

Here are the details about the error. 以下是有关错误的详细信息。

An exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll but was not handled in user code

Additional information: Could not find a part of the path 'C:\App_Data\books.xml'.

Here is the Default.aspx code for reference: 这是Default.aspx代码供参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace LibraryShelf
{
    public partial class Default : System.Web.UI.Page
    {
        //protected void Page_Load(object sender, EventArgs e)
        //{

        //}

        static void addNode(string fileName, XmlDocument xmlDoc)
        {
            XmlElement bookElement = xmlDoc.CreateElement("book");
            bookElement.SetAttribute("name", "DotNet Made Easy");

            XmlElement authorElement = xmlDoc.CreateElement("author");
            authorElement.InnerText = "microsoft";
            bookElement.AppendChild(authorElement);

            XmlElement priceElement = xmlDoc.CreateElement("price");
            priceElement.InnerText = "50";
            bookElement.AppendChild(priceElement);

            xmlDoc.DocumentElement.AppendChild(bookElement);
            xmlDoc.Save(fileName);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = System.IO.Path.Combine(Request.ApplicationPath, "App_Data/books.xml");

            XmlTextReader _xmlTextReader = new XmlTextReader(fileName);
            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.Load(_xmlTextReader);

            //Note: Close the reader object to release the xml file. Else while saving you will get an error that it is
            //being used by another process.
            _xmlTextReader.Close();

            addNode(fileName, _xmlDocument);
        }
    }
}

The application folder is not the physical path to the web site, it's the path from the domain root to the application root. 应用程序文件夹不是网站的物理路径,而是从域根目录到应用程序根目录的路径。 This is usually an empty string, unless you are using a virtual directory or an application subfolder on the server. 除非您在服务器上使用虚拟目录或应用程序子文件夹,否则通常为空字符串。

Use the MapPath method to get the physical path of a virtual address: 使用MapPath方法获取虚拟地址的物理路径:

string fileName = Server.MapPath("~/App_Data/books.xml");

try this - 尝试这个 -

string path = System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/books.xml"));

Server.MapPath will get you the location of the file. Server.MapPath将为您提供文件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM