English 中文(简体)
修改申请。
原标题:Modifying App.Config file at the time of installation using c#
  • 时间:2010-08-31 11:42:40
  •  标签:
  • c#
<configuration>
  <configSections>
    <section name="ADMIN" type="System.Configuration.DictionarySectionHandler"/>
  </configSections>
  <User>    
    <add key="ExtendTime" value="20"/>
    <add key="Name" value="sss"/>
  </User>
<configuration>

i 必须删除用户组合的第一类儿童内容。 如果你对此有任何想法,请回答。

i 使用

Configuration config = ConfigurationManager.OpenExeConfiguration(Context.Parameters["assemblypath"]);
ConfigurationSection section = config.GetSection("USER");
问题回答

本条可参考以下网址:

摘自:

string exePath = string.Format("{0}MyWindowsFormsApplication.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["Param1"].Value = param1;
config.AppSettings.Settings["Param2"].Value = param2;
config.AppSettings.Settings["Param3"].Value = param3;
config.Save();

EDIT: 添加编码样本和博客参考资料:

using System;
using System.Xml;  
using System.Configuration;
using System.Reflection;
//...


public class ConfigSettings
{
    private ConfigSettings() {}

    public static string ReadSetting(string key)
    {
        return ConfigurationSettings.AppSettings[key];
    }

    public static void WriteSetting(string key, string value)
    {
        // load config document for current assembly
        XmlDocument doc = loadConfigDocument();

        // retrieve appSettings node
        XmlNode node =  doc.SelectSingleNode("//appSettings");

        if (node == null)
            throw new InvalidOperationException("appSettings section not found in config file.");

        try
        {
            // select the  add  element that contains the key
            XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key= {0} ]", key));

            if (elem != null)
            {
                // add value for key
                elem.SetAttribute("value", value);
            }
            else
            {
                // key was not found so create the  add  element 
                // and set it s key/value attributes 
                elem = doc.CreateElement("add");
                elem.SetAttribute("key", key);
                elem.SetAttribute("value", value); 
                node.AppendChild(elem);
            }
            doc.Save(getConfigFilePath());
        }
        catch
        {
            throw;
        }
    }

    public static void RemoveSetting(string key)
    {
        // load config document for current assembly
        XmlDocument doc = loadConfigDocument();

        // retrieve appSettings node
        XmlNode node =  doc.SelectSingleNode("//appSettings");

        try
        {
            if (node == null)
                throw new InvalidOperationException("appSettings section not found in config file.");
            else
            {
                // remove  add  element with coresponding key
                node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key= {0} ]", key)));
                doc.Save(getConfigFilePath());
            }
        }
        catch (NullReferenceException e)
        {
            throw new Exception(string.Format("The key {0} does not exist.", key), e);
        }
    }

    private static XmlDocument loadConfigDocument()
    {
        XmlDocument doc = null;
        try
        {
            doc = new XmlDocument();
            doc.Load(getConfigFilePath());
            return doc;
        }
        catch (System.IO.FileNotFoundException e)
        {
            throw new Exception("No configuration file found.", e);
        }
    }

    private static string getConfigFilePath()
    {
        return Assembly.GetExecutingAssembly().Location + ".config";
    }
}

之后,你会利用这一方法:

// read the Test1 value from the config file
string test1 = ConfigSettings.ReadSetting("Test1");

// write a new value for the Test1 setting
ConfigSettings.WriteSetting("Test1", "This is my new value");

// remove the Test1 setting from the config file
ConfigSettings.RemoveSetting("Test1");

我得出结论,在安装期间无法使用以下设备进入一个定制科:

MyCustomConfigurationSection section = (MyCustomConfigurationSection)config.GetSection("MyCustomConfigurationSection");

在安装MSI包时,所执行的方案是Windows Install(MsiExec),而不是包含安装舱的方案。

 %windir%system32msiexec.exe

为了接触这场会议,我们需要利用具体情况来解决这一问题:

Configuration config = ConfigurationManager.OpenExeConfiguration(this.Context.Parameters["assemblypath"]);

或者,通过思考,检索执行大会的地点:

Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

根据所建议,你可以进入申请人,并修改:

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings["Environment"].Value = _Environment;
config.Save();

这是罚款,因为安装商知道如何处理。

System.Configuration.AppSettingsSection

由于该图书馆是因特网的一部分。 然而,在涉及海关部门时,安装商需要知道如何处理这一习俗。 很可能,在你申请中提到的一间图书馆(DLL)内安装了DL,现在安装在安装名录上。

正如我们从上文所知道的那样,问题在于: 在该名录中,Exec.exe isn t的运行,因此该装置在系统32中无法找到适当的DLL时就失效了。

An error occurred creating the configuration section handler for XXX : Could not load file or assembly XXX.dll or one of its dependencies. The system cannot find the file specified.

因此,查阅习俗集束的唯一途径是将汇辑作为XML文件处理,并利用传统的XML管理工具加以编辑:

// load the doc
XmlDocument doc = new XmlDocument();
doc.Load(Assembly.GetExecutingAssembly().Location + ".config");

// Get the node
XmlNode node =  doc.SelectSingleNode("//MyCustomConfigurationSection");

// edit node here
// ...

// Save
doc.Save(Assembly.GetExecutingAssembly().Location + ".config");

http://ryanfarley.com/blog/archive/2004/07/13/879.aspx”rel=“nofollow” 瑞安·法利·博克·,正如在对其原始答复的评论中指出的。

Good news! I have found a way how to work-around this problem. Solution is to intercept loading of assembly 以及 return one we have. To do so

ResolveEventH以及ler h以及ler = new ResolveEventH以及ler(CurrentDomain_AssemblyResolve);
AppDomain.CurrentDomain.AssemblyResolve += h以及ler;
try
{
   section = config.GetSection("mySection") as MySection;
}
catch(Exception)
{
}
AppDomain.CurrentDomain.AssemblyResolve -= h以及ler;

以及

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "Dead.Beef.Rocks")
    {
        return typeof(MySection).Assembly;
    }
    return null;
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签