存放地点"出厂默认值"配置设置,因此可以通过编程方式更新

时间:2015-08-05 19:42:30

标签: c# visual-studio-2010

我的Visual Studio应用程序的用户可能会单击按钮将应用程序恢复为出厂默认设置。在工厂,我们配置应用程序,然后单击另一个按钮将该配置设置为出厂默认设置为当前配置设置。

但是,如果我们将配置设置(XML格式)保存到settings.settings,它们将存储在我们自己的用户文件夹中(而不是Visual Studio项目文件夹中),并且用户不会收到它们。< / p>

(settings.settings使用设计时存储的默认值。)

我们需要将出厂默认值存储在可执行文件中包含的文件中,或随其一起分发。我们可以在发行版附带的factorydefaultconfig.xml文件中编写出厂默认值,但我认为您可能知道更好的方法。

现在我正在研究应用程序配置文件以查看我应该使用的是什么。

2 个答案:

答案 0 :(得分:2)

Yep App设置或滚动自己是我们过去处理此问题的方式。将System.Configuration的引用添加到项目中,然后使用以下命令:

ConfigurationManager.AppSettings.Set("lang", "English"); //Set
string getLang = ConfigurationManager.AppSettings["lang"]; //Get

对于App.config:

<configuration>
  <appSettings>
    <add key="lang" value="English"/>
  </appSettings>
</configuration>

答案 1 :(得分:1)

以防万一你想尝试一下。这是我写的一个名为ConfigHub的类,用于完成您所说的内容。它也使用锁,以确保您不会遇到文件使用错误:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Xml;

public static class ConfigHub
{
    #region State
    private static string WorkingDirectoryVar = null;
    private static string ConfigFileNameVar = null;
    private static bool AutoRefreshVar = true;
    private static bool VerboseVar = true;
    private static bool SetupExecutedVar = false;
    private static XmlDocument ConfigDocVar = new XmlDocument();
    private static Dictionary<string, string> ConfigLookupPair = new Dictionary<string, string>();
    private static Object ConfigHubLock = new Object();
    private const string CommentNameVar = "#comment";
    #endregion

#region Property
public static bool Verbose
{
    get { return VerboseVar; }
    set { VerboseVar = value; }
}
public static bool AutoRefresh
{
    get { return AutoRefreshVar; }
    set { AutoRefreshVar = value; }
}
public static bool SetupExecuted
{
    get { return SetupExecutedVar; }
}
public static string ConfigFilePath
{
    get { return WorkingDirectoryVar + @"\" + ConfigFileNameVar; }
}
public static string ConfigFileName
{
    get { return ConfigFileNameVar; }
}
public static string WorkingDirectory
{
    get { return WorkingDirectoryVar; }
}
#endregion

#region Setup
public static void Setup()
{
    lock (ConfigHubLock)
    {
        //Initialize config with default
        WorkingDirectoryVar = Environment.CurrentDirectory;
        ConfigFileNameVar = "SCW.Config.xml";
        SetupExecutedVar = true;
        RefreshConfiguration();
    }
}
public static void Setup(string configFileName)
{
    lock (ConfigHubLock)
    {
        //Initialize config with specified file
        WorkingDirectoryVar = Environment.CurrentDirectory;
        ConfigFileNameVar = configFileName.Trim().ToLower().Replace(".xml", "") + ".xml";
        SetupExecutedVar = true;
        RefreshConfiguration();
    }
}
#endregion

#region Merchant
public static void SetValue(string key, string value)
{
    //Fail if setup hasn't been called
    if (!SetupExecutedVar) throw ConfigHubException.BuildException(ConfigHubExceptionType.NotSetup, "Setup must be called before using the ConfigHub", null);        

    try
    {            
        lock (ConfigHubLock)
        {
            //Set the value 
            bool foundNode = false;
            foreach (XmlNode configNode in ConfigDocVar.ChildNodes[0].ChildNodes)
            {
                if (configNode.Name.Trim().ToLower() == key.Trim().ToLower())
                {
                    configNode.InnerXml = value.Trim();
                    foundNode = true;
                }
            }
            if (!foundNode)
            {
                XmlNode newNode = ConfigDocVar.CreateNode("element", key.Trim(), "");
                newNode.InnerXml = value.Trim();
                ConfigDocVar.ChildNodes[0].AppendChild(newNode);
            }

            //Save the config file
            ConfigDocVar.Save(WorkingDirectoryVar + @"\" + ConfigFileNameVar);                
            RefreshConfiguration();
        }
    }
    catch (Exception err)
    {
        throw ConfigHubException.BuildException(ConfigHubExceptionType.SetValue, "Set value failed", err);
    }
}
public static string GetValue(string key)
{
    //Fail if setup hasn't been called
    if (!SetupExecutedVar) throw ConfigHubException.BuildException(ConfigHubExceptionType.NotSetup, "Setup must be called before using the ConfigHub", null);
    if (AutoRefreshVar) RefreshConfiguration();

    try
    {
        lock (ConfigHubLock)
        {
            //Get and return the value
            if (AutoRefreshVar) RefreshConfiguration();
            if (ConfigLookupPair.ContainsKey(key.Trim().ToLower()))
            {
                return ConfigLookupPair[key.Trim().ToLower()];
            }
            else
            {
                throw ConfigHubException.BuildException(ConfigHubExceptionType.NoKeyFound, "The key " + key + " was not found", null);
            }
        }
    }
    catch (Exception err)
    {
        throw ConfigHubException.BuildException(ConfigHubExceptionType.GetValue, "Get value failed", err);
    }
}
public static void RefreshConfiguration()
{
    //Fail if setup hasn't been called
    if (!SetupExecutedVar) throw ConfigHubException.BuildException(ConfigHubExceptionType.NotSetup, "Setup must be called before using the ConfigHub", null);

    try
    {
        //Load configuration from file
        ConfigDocVar.Load(WorkingDirectoryVar + @"\" + ConfigFileNameVar);
        List<string> duplicateCheck = new List<string>();
        foreach (XmlNode configNode in ConfigDocVar.ChildNodes[0].ChildNodes)
        {
            if (configNode.Name.Trim().ToLower() == CommentNameVar)
            {
                //Ignore the Comment
            }
            else
            {
                if (duplicateCheck.Contains(configNode.Name.Trim().ToLower()))
                {
                    //Duplicate key failure
                    throw ConfigHubException.BuildException(ConfigHubExceptionType.DuplicateKey, "The key " + configNode.Name.Trim() + " appears multiple times", null);
                }
                else
                {
                    //Add configuration key value pair
                    duplicateCheck.Add(configNode.Name.Trim().ToLower());
                    if (!ConfigLookupPair.ContainsKey(configNode.Name.Trim().ToLower()))
                    {
                        ConfigLookupPair.Add(configNode.Name.Trim().ToLower(), configNode.InnerXml.Trim());
                    }
                    else
                    {
                        ConfigLookupPair[configNode.Name.Trim().ToLower()] = configNode.InnerXml.Trim();
                    }
                }
            }
        }
    }
    catch (Exception err)
    {
        //Look form root missing and multiple roots
        if (err.ToString().ToLower().Contains("root element is missing"))
        {
            throw ConfigHubException.BuildException(ConfigHubExceptionType.NoRootFound, "No configuration root found", err);
        }
        else if (err.ToString().ToLower().Contains("multiple root elements"))
        {
            throw ConfigHubException.BuildException(ConfigHubExceptionType.MultipleRoots, "Multiple configuration roots found", err);
        }
        else
        {
            throw ConfigHubException.BuildException(ConfigHubExceptionType.Refresh, "Refresh failed", err);
        }
    }
}
#endregion    
}

#region Exception
public enum ConfigHubExceptionType { NotSetup, Setup, Refresh, DuplicateKey, NoKeyFound, SetValue, GetValue, NoRootFound, MultipleRoots }
public class ConfigHubException : Exception
{
    public ConfigHubException(ConfigHubExceptionType errType, string message) : base("#" + errType.ToString() + "-" + message + (ConfigHub.ConfigFilePath != @"\" ? " (" + ConfigHub.ConfigFilePath + ")" : "")) { }
    public ConfigHubException(ConfigHubExceptionType errType, string message, Exception innerException) : base("#" + errType.ToString() + "-" + message + (ConfigHub.ConfigFilePath != @"\" ? " (" + ConfigHub.ConfigFilePath + ")" : ""), innerException) { }
    public static ConfigHubException BuildException(ConfigHubExceptionType exceptionType, string message, Exception innerException)
    {
        if (!ConfigHub.Verbose || innerException == null) return new ConfigHubException(exceptionType, message);
        else return new ConfigHubException(exceptionType, message, innerException);      
    }
}
#endregion

请注意。您需要首先在项目中构建文件并删除XML标头。如下所示(例如:MyConfigFile.xml) - 顶部没有xml标签,如()。最后,确保将xml配置文件设置为始终复制:

<Config>
   <ValueName>Value</ValueName>
</Config>