gpt4 book ai didi

c# - 如何使设计器生成的 .Net 应用程序设置可移植

转载 作者:太空狗 更新时间:2023-10-29 22:10:08 26 4
gpt4 key购买 nike

我一直在考虑修改 Doppler podcast aggregator 的源代码目标是能够直接从我的 mp3 播放器运行程序。

Doppler 使用生成的 Visual Studio 设计器存储应用程序设置 Settings class ,默认情况下将用户设置序列化到用户的主目录。我想更改此设置,以便所有设置都存储在与 exe 相同的目录中。

这似乎可以通过创建一个继承 SettingsProvider 的自定义提供程序类来实现。类(class)。有没有人创建过这样的提供程序并愿意共享代码?

更新:通过使用此 MSDN sample,我能够几乎获得自定义设置提供程序,即简单继承。我最初感到困惑,因为 Windows 窗体设计器停止工作,直到我执行了 Codeproject 中建议的技巧。 :

internal sealed partial class Settings
{
private MySettingsProvider settingsprovider = new MySettingsProvider();

public Settings()
{
foreach (SettingsProperty property in this.Properties)
{
property.Provider = settingsprovider;
}
...

程序仍然以窗口大小 0 开始;不过 0。

有人对此有任何见解吗?

  • 为什么需要在运行时评估提供者——而不是像 MSDN 所建议的那样使用属性?
  • 为什么使用默认设置提供程序将默认设置传递给应用程序的方式与使用自定义提供程序的方式有所不同?

最佳答案

为什么不按原样使用 CodeProject PortableSettingsProvider 解决方案(进行一些小的更改)?我在我的项目 ( StreamRecorder.NET ) 中成功地这样做了。

项目页面上的一些评论很有用:

我最终得到的代码:

    static void Main(string[] args)
{
if (args.Contains("-p") || args.Contains("--portable"))
{
MakePortable(Properties.Settings.Default);
MakePortable(Properties.LastUsedSettings.Default);
MakePortable(Properties.DefaultSettings.Default);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(args));
}

private static void MakePortable(ApplicationSettingsBase settings)
{
var portableSettingsProvider =
new PortableSettingsProvider(settings.GetType().Name + ".settings");
settings.Providers.Add(portableSettingsProvider);
foreach (System.Configuration.SettingsProperty prop in settings.Properties)
prop.Provider = portableSettingsProvider;
settings.Reload();
}

最后,我对 CP 项目进行了以下更改:

string _fileName;
public PortableSettingsProvider(string fileName)
{
_fileName = fileName;
}

public virtual string GetAppSettingsFilename()
{
//Used to determine the filename to store the settings
//return ApplicationName + ".settings";
return _fileName;
}

关于c# - 如何使设计器生成的 .Net 应用程序设置可移植,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1382617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com