我可以有不同的“.settings”吗?指定用于调试和发布配置?

发布于 2024-08-31 03:38:57 字数 76 浏览 8 评论 0原文

我有一个 .settings 文件(包含用户和应用程序设置),如果在调试模式下构建,我希望包含不同的值。有支持或推荐的方法来执行此操作吗?

I have a .settings file (containing both User and Application settings) that I would like to contain different values if built in Debug mode. Is there a supported or recommended way to do this?

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

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

发布评论

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

评论(2

纸伞微斜 2024-09-07 03:38:57

我为每种模式使用两个单独的配置文件。我将文件复制到 POST-BUILD 事件中的 BIN 文件夹中。

I use two separate config files for each mode. I copy the files into the BIN folder in the POST-BUILD event.

网白 2024-09-07 03:38:57

Settings.Designer.cs 文件不包含值,仅包含应用程序设置属性声明。这些值是单独存储的。应用程序设置值进入 app.exe.config 文件,用户范围的设置值进入 appdata 文件夹,该文件夹的名称由哈希算法生成。你只会遇到后者的麻烦。没关系,当您将发布版本部署到计算机时,该文件将不存在。

如果您的意思是“我可以更改默认值”,那么答案是:当您使用设置设计器时不能。您必须将设置移至单独的类中。使其看起来与此类似:

using System;
using System.Configuration;
using System.Diagnostics;

namespace ConsoleApplication1.Properties {
    internal partial class Settings {
        [UserScopedSetting, DebuggerNonUserCode]
#if DEBUG
        [DefaultSettingValue("debug value")]
#else
        [DefaultSettingValue("release value")]
#endif
        public string Setting {
            get {
                return ((string)(this["Setting"]));
            }
            set {
                this["Setting"] = value;
            }
        }

    }
}

确保命名空间名称与 Settings.Designer.cs 文件中使用的名称匹配,并且从“设置”页面中删除该设置。

The Settings.Designer.cs file doesn't contain values, only application setting property declarations. The values are stored separately. Application setting values go into the app.exe.config file, user scoped setting values go in an appdata folder that has a name generated by a hashing algorithm. You'd only have trouble with the latter. Shouldn't matter, the file won't exist when you deploy your Release build to a machine.

In case you mean "can I change the default value" then the answer is: not when you use the settings designer. You'll have to move the setting into a separate class. Make it look similar to this:

using System;
using System.Configuration;
using System.Diagnostics;

namespace ConsoleApplication1.Properties {
    internal partial class Settings {
        [UserScopedSetting, DebuggerNonUserCode]
#if DEBUG
        [DefaultSettingValue("debug value")]
#else
        [DefaultSettingValue("release value")]
#endif
        public string Setting {
            get {
                return ((string)(this["Setting"]));
            }
            set {
                this["Setting"] = value;
            }
        }

    }
}

Make sure the namespace name matches the one used in the Settings.Designer.cs file and that you delete the setting from the Settings page.

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