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

发布于 2025-01-07 09:37:04 字数 504 浏览 0 评论 0原文

我的项目中有一个 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 的程序“版本”时再次填充整个设置然而。 (如果我不够清楚,我很乐意提供更多详细信息)

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

I have a Settings.cs file in my project, and I access the data in it from my program via

Properties.Settings.Default.MyProperty

The generated settings file is stored in the following location

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

The problem is that this is not only user specific, but it also results in the program having many user.config files for every signature (debug/release, etc.), which forces the developer-user to populate the whole settings again each time he launches a "version" of the program that does not have a specific user.config yet. (If I am not being clear enough, I'll be glad to give more details)

I would like my application to have a single settings files for all users and no matter the "version" (debug/release, or else). This way, the dev-user would have to set the settings one single time and these settings would be effective each time the application is launched, without the need to re-enter them for the other signatures/users.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一笔一画续写前缘 2025-01-14 09:37:04

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

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)
            }
        }

另一个选择是创建一个可序列化的类([ Serialized()] 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;
}

You can save and read setting like all advanced programs in Registry, and that is how to do it:

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)
            }
        }

Another choice you have that you make a serializable class ([Serializable()] attrib) that contains all of your settings as properties, then save it in your app directory, with the BinaryFormatter class.

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;
}
如梦初醒的夏天 2025-01-14 09:37:04

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

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 语法。

I have devised a simple solution with some drawbacks:

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; }
}

Problem: You need UAC assense to write over your executable config and you can't use Properties.Settings.Default.MyProperty syntax.

往日情怀 2025-01-14 09:37:04

我最终选择了一种更简单直接的替代方案,包括创建一个普通的 Settings 类并按照此处所述对其进行序列化/反序列化

I Finally opted for a simpler straightforward alternative, consisting in creating a plain Settings class and Serializing/Deserializing it as described here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文