大侠请看下面的代码:
public void WebConfig_Mod(ArrayList newinfo,string docName)
{
try
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(docName);
XmlNode node = doc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new InvalidOperationException( "appSettings section not found");
}
XmlNodeList xnl = node.ChildNodes;
for(int i=0;i<xnl.Count;i++)
{
XmlElement Elem= (XmlElement)xnl[i];
Elem.SetAttribute("value",newinfo[i].ToString());
}
XmlTextWriter writer = new XmlTextWriter(docName,null);
writer.Formatting = Formatting.Indented;
doc.WriteTo( writer );
writer.Flush();
writer.Close();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}这段代码前面运行都没有问题,只是到了XmlTextWriter writer = new XmlTextWriter(docName,null);
就会提示错误:{@"对路径“C:\Inetpub\wwwroot\Test\web.config”的访问被拒绝。" }
我不知道是什麽原因,都折腾一上午了。对了,写入权限是有的,各位大侠有什麽高见?
public void WebConfig_Mod(ArrayList newinfo,string docName)
{
try
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(docName);
XmlNode node = doc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new InvalidOperationException( "appSettings section not found");
}
XmlNodeList xnl = node.ChildNodes;
for(int i=0;i<xnl.Count;i++)
{
XmlElement Elem= (XmlElement)xnl[i];
Elem.SetAttribute("value",newinfo[i].ToString());
}
XmlTextWriter writer = new XmlTextWriter(docName,null);
writer.Formatting = Formatting.Indented;
doc.WriteTo( writer );
writer.Flush();
writer.Close();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}这段代码前面运行都没有问题,只是到了XmlTextWriter writer = new XmlTextWriter(docName,null);
就会提示错误:{@"对路径“C:\Inetpub\wwwroot\Test\web.config”的访问被拒绝。" }
我不知道是什麽原因,都折腾一上午了。对了,写入权限是有的,各位大侠有什麽高见?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml ;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//打开某文件(假设WEB。CONFIG在根目录中)
string filename=Server.MapPath("/") + @"\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{
XmlNodeList _node=element.ChildNodes;
if ( _node.Count >0 )
{
DropDownList1.Items.Clear();
foreach(XmlElement el in _node)
{
DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
}
}
}
}
}
}private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);}
#endregionprivate void Button1_Click(object sender, System.EventArgs e)
{
string filename=Server.MapPath("/") + @"\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{
XmlNodeList _node=element.ChildNodes;
if ( _node.Count >0 )
{
foreach(XmlElement el in _node)
{
if(el.Attributes["key"].InnerXml.ToLower()==this.DropDownList1.SelectedItem.Value.ToLower())
{
el.Attributes["value"].Value=this.TextBox1.Text;
}
}
}
}
}
xmldoc.Save(filename);
}
}
}
有没有人知道是为什么?是不是根本不能动态修改web.config呀?遇到过这类问题的人帮帮忙吧!在线等……
xjliang007(痛并快乐着) :你说不能修改有什么根据吗?还是你试过了肯定不能改?
如果你的代码是在Windows应用程序中,出现访问被拒绝的问题应该是你运行你的Windows程序的操作系统用户没有对web.config的写入权限
这个是被系统在运行时监控的. 难道你想每次都让网站重新启动一下吗?建议不要作类似的工作啊.当然,要非这么干,也不要在网站本身中去做,写个外部的exe程序,去强行修改吧.
是其它,估計是不行的,最少用戶信息等都不會存在了.樓主是不是要重新登錄呢?個人認為,動態更
改Web.Config只是理論上的可行,實際是沒有意義的,不知道LZ
------------------------------------
谁说的!
这是不是说明问题确实出在权限上?可是,我已经将IIS中Test的属性选上了“读取”和“写入”,但是C:\Inetpub\wwwroot\路径下的Test文件夹的属性是“只读”。不知道问题究竟出在哪里,我还应该修改谁的权限呢?
还请各位高手再指教一下~~
在VS 2005下,配置文件的整个模型发生了改变,应该是可以比较方便的修改配置文件了吧。
本想结贴的,但是看到大家讨论的如此热烈,就再放几天吧^_^
但以前的进程会结束,IIS会重新启动一个新的进程,所以,以前的内存都将清理掉。
这是一个很好的整理内存的方法(以前我的项目不知道一个什么原因,要IIS重新启动,没办法,我就修改一下config,让IIS自己重新启动进程,当然,后来修改了错误)。
动态修改文件是可以的,但要注意你的程序运行时,有两个书用户账号你要注意一下:
一个是你的计算机名+XX(IIS的运行账号),这是你的IIS进程运行的账号,一般,这个账号可以对wwwroot里的所有文件都有完全操作权限,当然如果出现设置错误就另当别论了。
另一个账号就是IIS来宾账号,它是网络上访问Web的账号,它不能对文件进行操作的,请检查一下这两个账号。
所以:如果你要修改文件,请一定用IIS进程账号。