在.NET5WinForms应用程序中,设置保存在哪里? [英] Where are the settings saved in a .NET 5 WinForms app?
本文介绍了在.NET5WinForms应用程序中,设置保存在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在.NET框架WinForms项目中,项目中有一个App.config文件,该文件是一个XML文件,其中包含一个引用System.Configuration中的类的configSection,以及一个用于用户设置本身的节,如下所示:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089">
<section name="MyAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyAppName.Properties.Settings>
<setting name="Test" serializeAs="String">
<value>Some Value</value>
</setting>
</MyAppName.Properties.Settings>
</userSettings>
这将在Build文件夹中创建一个应用程序名称加上.exe.config的文件,如MyAppName.exe.config。
但当我使用.NET创建新的WinForms项目时:
解决方案中没有App.config。我可以使用项目属性编辑设置:
我可以访问这些值,并使用相同的Properties对象和方法更新它们:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = Properties.Settings.Default.Test;
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Test = textBox1.Text;
Properties.Settings.Default.Save();
}
}
}
一切似乎都正常,但当我检查bin文件夹时,没有看到实际存储值的文件。
如果保存的应用程序设置不在与应用程序的EXE相同的文件夹中的文件中,.NET 5将存储在哪里?
推荐答案
用户设置存储在user.config
文件中,路径如下:
%userprofile%appdatalocal<Application name><Application uri hash><Application version>
应用程序设置文件在默认情况下不会创建(意外),但是,如果您在应用程序的dll/exe文件旁边手动创建它们,则配置系统会考虑该文件。文件名应为<Application name>.dll.config
。请注意文件扩展名.dll.config
。
您可能需要查看以下类的源代码:
- LocalFileSettingsProvider(默认设置提供程序)
- ClientSettingsStore
- ConfigurationManagerInternal
- ClientConfigurationPaths
在撰写此答案时Application Settings for Windows Forms仍然没有任何.NET 5条目,并重定向到4.x文档。
这篇关于在.NET5WinForms应用程序中,设置保存在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文