将应用程序设置存储在项目文件夹而不是AppData中

时间:2012-02-21 09:32:33

标签: c# visual-studio-2010

我的项目中有一个Settings.cs文件,我通过

从我的程序中访问其中的数据
Properties.Settings.Default.MyProperty

生成的设置文件存储在以下位置

C:\Users\Foo\AppData\Local\MyApp\MyApp.exe_Url_jknwq2raeohczydfp1loj02nf05zldfk\1.0.0.0\user.config

问题是这不仅是用户特定的,而且还导致程序为每个签名(调试/发布等)提供了许多user.config文件,这迫使开发人员用户填充整个设置每次他启动一个没有特定user.config的程序的“版本”时再次。 (如果我不够清楚,我会很乐意提供更多细节)

我希望我的应用程序为所有用户提供单个设置文件,无论是“版本”(调试/发布,否则)。这样,开发者用户必须一次设置设置,并且每次启动应用程序时这些设置都有效,而无需为其他签名/用户重新输入这些设置。

4 个答案:

答案 0 :(得分:1)

只需使用具有应用程序范围的设置。

答案 1 :(得分:1)

您可以像注册表中的所有高级程序一样保存和阅读设置,具体方法如下:

public object GetRegistryValue(string KeyName, object DefaultValue)
        {
            object res = null;
            try
            {
                Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer();
                Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true);
                if (k != null)
                {
                    res = k.GetValue(KeyName, DefaultValue);
                }
                else
                {
                    k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName");
                }
                if (k != null)
                    k.Close();
                // ex As Exception
            }
            catch
            {
                //PromptMsg(ex)
            }
            return res;
        }

public void SetRegistryValue(string KeyName, object _Value)
        {
            try
            {
                Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer();

                Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true);
                if (k != null)
                {
                    k.SetValue(KeyName, _Value);
                }
                else
                {
                    k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName");
                    k.SetValue(KeyName, _Value);
                }
                if (k != null)
                    k.Close();
                // ex As Exception
            }
            catch
            {
                //PromptMsg(ex)
            }
        }

您有另一个选择,您可以创建一个可序列化的类( [Serializable()] attrib),其中包含您作为属性的所有设置,然后将其保存在您的应用中目录,使用BinaryFormatter类。

public void saveBinary(object c, string filepath)
{
    try
    {
        using (System.IO.Stream sr = System.IO.File.Open(filepath, System.IO.FileMode.Create))
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            bf.Serialize(sr, c);
            sr.Close();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public object loadBinary(string path)
{
    try
    {
        if (System.IO.File.Exists(path))
        {
            using (System.IO.Stream sr = System.IO.File.Open(path, System.IO.FileMode.Open))
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                object c = bf.Deserialize(sr);
                sr.Close();
                return c;
            }
        }
        else
        {
            throw new Exception("File not found");
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return null;
}

答案 2 :(得分:1)

我设计了一个简单的解决方案,但有一些缺点:

public void WriteLocalValue(string localKey, string curValue)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    KeyValueConfigurationElement k = config.AppSettings.Settings[localKey];
    if (k == null)
        config.AppSettings.Settings.Add(localKey, curValue);
    else
        k.Value = curValue;
    config.Save();
}

public string ReadLocalValue(string localKey, string defValue)
{
    string v = defValue;
    try
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration( Application.ExecutablePath);
        KeyValueConfigurationElement k = config.AppSettings.Settings[localKey];
        if (k != null) v = (k.Value == null ? defValue : k.Value);
            return v;
    }
    catch { return defValue; }
}

问题:您需要UAC assense来覆盖您的可执行配置,并且您不能使用Properties.Settings.Default.MyProperty语法。

答案 3 :(得分:0)

我终于选择了一个更简单直接的替代方案,包括创建一个普通的Settings类并按照here所述序列化/反序列化它