首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

FileStream 未能找出路径的一部分。asp.net

2012-08-14 
FileStream 未能找到路径的一部分。asp.net代码如下,直接本地测试就出现错误。.net3.5的环境C# codeusing Sy

FileStream 未能找到路径的一部分。asp.net
代码如下,直接本地测试就出现错误。.net3.5的环境

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;using System.IO;public partial class _Default : System.Web.UI.Page {    private string file;    protected void Page_Load(object sender, EventArgs e)    {        file = Path.Combine(Request.PhysicalPath, @"App_Data\SuperProductiList.xml");    }       protected void Button1_Click(object sender, EventArgs e)    {                FileStream fs = new FileStream(file, FileMode.Create);        XmlTextWriter w = new XmlTextWriter(fs, null);        w.WriteStartDocument();        w.WriteStartElement("Super");        w.WriteComment("This file generated by the XmlTextWriter class.");        w.WriteStartElement("Product");        w.WriteAttributeString("ID", "1");        w.WriteAttributeString("Name", "Chair");        w.WriteStartElement("price");        w.WriteString("49.5");        w.WriteEndElement();        w.WriteEndElement();        w.WriteEndElement();        w.WriteEndDocument();        w.Close();    }}


[解决办法]
你看看
Request.PhysicalPath, @"App_Data\SuperProductiList.xml")
出来的路径是什么吧

是不存在的,。

直接
file = Server.MapPath("~/App_Data/SuperProductiList.xml");

SuperProductiList.xml放在根目录的app_Data下面
[解决办法]
file = Path.Combine(Request.PhysicalPath, @"App_Data\SuperProductiList.xml");
Request.PhysicalPath

换成:
AppDomain.CurrentDomain.BaseDirectory
file = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\SuperProductiList.xml";

热点排行