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

用户代码是否未处理DirectoryNotFoundException?(Visual Studio 2013,C#),c#,C#,当我试着运行这个程序时,当我点击一个按钮时,它会将一本书的详细信息写入一个xml文件,我会得到一个DirectoryNotFoundException。如何重写App_数据文件夹下books.xml文件的地址 以下是有关错误的详细信息 An exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll but was not handled in user code Additional

当我试着运行这个程序时,当我点击一个按钮时,它会将一本书的详细信息写入一个xml文件,我会得到一个DirectoryNotFoundException。如何重写App_数据文件夹下books.xml文件的地址

以下是有关错误的详细信息

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'.
以下是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);
        }
    }
}

应用程序文件夹不是网站的物理路径,而是从域根目录到应用程序根目录的路径。这通常是一个空字符串,除非您使用的是服务器上的虚拟目录或应用程序子文件夹

使用
MapPath
方法获取虚拟地址的物理路径:

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

应用程序文件夹不是网站的物理路径,而是从域根目录到应用程序根目录的路径。这通常是一个空字符串,除非您使用的是服务器上的虚拟目录或应用程序子文件夹

使用
MapPath
方法获取虚拟地址的物理路径:

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

应用程序文件夹不是网站的物理路径,而是从域根目录到应用程序根目录的路径。这通常是一个空字符串,除非您使用的是服务器上的虚拟目录或应用程序子文件夹

使用
MapPath
方法获取虚拟地址的物理路径:

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

应用程序文件夹不是网站的物理路径,而是从域根目录到应用程序根目录的路径。这通常是一个空字符串,除非您使用的是服务器上的虚拟目录或应用程序子文件夹

使用
MapPath
方法获取虚拟地址的物理路径:

string fileName = Server.MapPath("~/App_Data/books.xml");
试试这个-

string path = System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/books.xml"));
Server.MapPath
将为您获取文件的位置。

请尝试以下操作-

string path = System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/books.xml"));
Server.MapPath
将为您获取文件的位置。

请尝试以下操作-

string path = System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/books.xml"));
Server.MapPath
将为您获取文件的位置。

请尝试以下操作-

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

Server.MapPath
将为您获取文件的位置。

您是否有
App\u Data
文件夹?您确定目录和文件存在吗?!编写代码:您应该按照在中声明和实例化XmlTextReader。您是否有
App_Data
文件夹?那么,您确定目录和文件存在吗?!编写代码:您应该按照在中声明和实例化XmlTextReader。您是否有
App_Data
文件夹?那么,您确定目录和文件存在吗?!编写代码:您应该按照在中声明和实例化XmlTextReader。您是否有
App_Data
文件夹?那么,您确定目录和文件存在吗?!编写您的代码:您应该按照在中声明和实例化XmlTextReader。