没试过..ConfigurationSettings.AppSettings["Application Name"] = "xx";行吗?

解决方案 »

  1.   

    只能够不读取。目前我还没有看到.net自己提供的写config文件的类。当然,你可以把他当作一般的xml文件,进行修改操作。上面的代码已经演示了如何使用。
      

  2.   

    string appName = ConfigurationSettings.AppSettings["Application Name"];
    上面这句话怎么理解?["Application Name"]是从哪个config文件读出来的呀?
      

  3.   

    string appName = ConfigurationSettings.AppSettings["Application Name"];上面的问法不对,我是说要到哪个文件中去找key为“Application Name”的value 值??而上面什么都没有说明
      

  4.   

    假设你在1.exe中使用了
    string appName = ConfigurationSettings.AppSettings["Application Name"];
    那么,你的config文件的就是1.exe.config
      

  5.   

    在程序里是不能修改web.config或app.exe.config的,但我记得看到过:在vs.net的安装部署项目里提供对修改web.config或app.exe.config的支持,具体内容我当时每看。你自己查查help之类的。
      

  6.   

    可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
    {
    try
    {
    string path=WebConfigDirectory+"\\web.config";
    XmlDocument xd=new XmlDocument();
    xd.Load(path); //如果没有appSetting,则添加
    if(xd.SelectNodes("//appSettings").Count==0)
    {
    xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
    } //判断节点是否存在,如果存在则修改当前节点
    bool addNode=true;
    foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
    {
    if(xn1.Attributes["key"].Value==appSettingsAddkey)
    {
    addNode=false;
    xn1.Attributes["value"].Value=keyvalue;
    // xn1.ParentNode.RemoveChild(xn1);
    break;
    }
    } //当前节点不存在,则添加新节点
    if(addNode)
    {
    //创建新节点
    XmlNode xn2=xd.CreateElement("add"); //添加key
    XmlAttribute xa=xd.CreateAttribute("key");
    xa.Value=appSettingsAddkey;
    xn2.Attributes.Append(xa); //添加value
    xa=xd.CreateAttribute("value");
    xa.Value=keyvalue;
    xn2.Attributes.Append(xa);
    xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
    }
    //保存web.config
    xd.Save(path);
    return true;
    }
    catch
    {
    return false;
    }
    }
      

  7.   

    可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
    {
    try
    {
    string path=WebConfigDirectory+"\\web.config";
    XmlDocument xd=new XmlDocument();
    xd.Load(path); //如果没有appSetting,则添加
    if(xd.SelectNodes("//appSettings").Count==0)
    {
    xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
    } //判断节点是否存在,如果存在则修改当前节点
    bool addNode=true;
    foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
    {
    if(xn1.Attributes["key"].Value==appSettingsAddkey)
    {
    addNode=false;
    xn1.Attributes["value"].Value=keyvalue;
    // xn1.ParentNode.RemoveChild(xn1);
    break;
    }
    } //当前节点不存在,则添加新节点
    if(addNode)
    {
    //创建新节点
    XmlNode xn2=xd.CreateElement("add"); //添加key
    XmlAttribute xa=xd.CreateAttribute("key");
    xa.Value=appSettingsAddkey;
    xn2.Attributes.Append(xa); //添加value
    xa=xd.CreateAttribute("value");
    xa.Value=keyvalue;
    xn2.Attributes.Append(xa);
    xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
    }
    //保存web.config
    xd.Save(path);
    return true;
    }
    catch
    {
    return false;
    }
    }
      

  8.   

    可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
    {
    try
    {
    string path=WebConfigDirectory+"\\web.config";
    XmlDocument xd=new XmlDocument();
    xd.Load(path); //如果没有appSetting,则添加
    if(xd.SelectNodes("//appSettings").Count==0)
    {
    xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
    } //判断节点是否存在,如果存在则修改当前节点
    bool addNode=true;
    foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
    {
    if(xn1.Attributes["key"].Value==appSettingsAddkey)
    {
    addNode=false;
    xn1.Attributes["value"].Value=keyvalue;
    // xn1.ParentNode.RemoveChild(xn1);
    break;
    }
    } //当前节点不存在,则添加新节点
    if(addNode)
    {
    //创建新节点
    XmlNode xn2=xd.CreateElement("add"); //添加key
    XmlAttribute xa=xd.CreateAttribute("key");
    xa.Value=appSettingsAddkey;
    xn2.Attributes.Append(xa); //添加value
    xa=xd.CreateAttribute("value");
    xa.Value=keyvalue;
    xn2.Attributes.Append(xa);
    xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
    }
    //保存web.config
    xd.Save(path);
    return true;
    }
    catch
    {
    return false;
    }
    }
      

  9.   

    我记得程序在运行的时候,自己是不能修改自己的config配置文件的呀,也许是我记错了,不过,逻辑上将也不应该让修改呀,比如web.config,aps.net只要感知应用程序的web.config被修改了,就会在接下来的请求到来时自动重启该web应用程序。
      如果上面是对的话(不会错吧),应用程序如何能修改自己的配置呢?
    请 xhan2000(popeye.net) 再解释一下好吗?
      

  10.   

    我同意xhan2000 的code, 因为.net 其实并不想让程序修改config, config 是为了让system administrator 在不用修改程序source code 和re-compile 的情况下改变程序的行为所以要改的话需要把它当一般的xml document 造DOM(或SAX)修改后再写回文件系统我觉得如果在config 里面加一条描述config file 本身路径的node 就更灵活了
    例如
    <add key="ConfigFilePath" value = "MyApp.exe.config" />
    然后先把文件名读进程序,
    string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFilePath");然后再 
    XmlDocument doc = new XmlDocument();
    doc.Load(filename);
    ...
    再对它做processing
      

  11.   

    csharpguy(csharpguy) 你也实际测试一下好吧,我在上个项目中也试图在程序修改配置文件,查了一些资料后,看到我上面说的那些内容,就没有深究而打消了自己在程序修改的主意,我想实实在弄清这个问题,麻烦测试后并解释一下,谢谢!
      

  12.   

    我试了一下,程序如下
    private void button1_Click(object sender, System.EventArgs e)
    {
    string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFileName");XmlDocument config = new XmlDocument();
    FileStream fs = new FileStream(filename, FileMode.Open);config.Load(fs);
    fs.Flush();
    fs.Close();string xpath = "//add[@key='textBox1.Text']";
    XmlNode node = config.DocumentElement.SelectSingleNode(xpath);
    XmlNode valueAtt = node.Attributes.GetNamedItem("value");
    if(valueAtt!=null)
      valueAtt.Value = "new value";FileStream wfs = new FileStream(filename, FileMode.Truncate);
    config.Save(wfs);
    wfs.Flush();
    wfs.Close();
    }这是我的 writeConfig.exe 的config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="textBox1.Text" value="my value" />
        <add key="ConfigFileName" value="writeConfig.exe.config" />
      </appSettings>
    </configuration>
      

  13.   

    程序运行后config 里 my value 变成 new value
      

  14.   

    这个是实际项目中截取的代码吧,我没法copy下来测试。Web应用程序测试过吗?
      

  15.   

    那是我随便放个button 在一个form 上然后写的 click event handler
    是winform application 的
    没有试过web config...
      

  16.   

    不能直接用System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"]=xxx来进行修改,这个我试过。用XML来修改我倒没试过,学习。
      

  17.   

    我改了这句,测试通过:
    string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"];现在关心Web.config是否也可这样修改,我继续测试看看。
      

  18.   

    web应用也测试了,程序运行没问题,但修改不了!好,搞清楚了。有收获,谢谢
      

  19.   

    我试了 web config, 也行,但要在 web.config 里加
    <identity impersonate="true" />当然还要把 Url 转换成 local pathjjcccc(): 我上面的code 只是一个button 的click handler,你要试的话,要把method 里面的内容copy 到你自己的event handler 里
      

  20.   

    我试过winform application 的代码
    string filename = System.Configuration.ConfigurationSettings.AppSettings.Get("ConfigFileName");这句话返回值为null,
    造成下面语句出错
    FileStream fs = new FileStream(filename, FileMode.Open);
    提示“路径不能为空”,应如何改,请指教
      

  21.   

    上面已经都说了:string filename = System.Configuration.ConfigurationSettings.AppSettings.Get("ConfigFileName");
    ==》
    string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"];
      

  22.   

    这句不是问题所在,
    问题是在config file 里
    你要自己加
    <appSettings>
    <add key="ConfigFileName" value="app.exe.config" />
    <appSettings/>这样 AppSettings.Get 才能找到 "app.exe.config" 这个值,再给filename 变量
      

  23.   

    app.exe.config 应该是你的application 的config 文件名
      

  24.   

    项目要用Release配置编译,且app.exe.config 要放到bin\release目录下。