我确实无法理解为什么我的 app.exe 没有获取 app.exe.config 中的更新值
应用程序配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<applicationSettings>
<appName.AppProperties>
<setting name="file_location" serializeAs="String">
<value>C:\users\test\desktop\file_location.csv</value>
</setting>
</appName.AppProperties>
</applicationSettings>
</configuration>
我认为一旦我重新启动应用程序(UAT,Prod),我的应用程序就会读取新位置。 知道为什么 file_location 仍然相同(即缓存)吗?
也许还有一种替代方法,在 appSettings 下创建一个密钥。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<add key="file_location" value="C:\users\test\desktop\file_location.csv"/>
</appSettings>
</configuration>
将以下类添加到您的项目中,该项目具有
file_location
的获取和设置。
using System;
using System.Configuration;
using System.IO;
using static System.Configuration.ConfigurationManager;
namespace YourNamespace
{
public class ApplicationSettings
{
public static void SetFileLocation(string fileName) => SetValue("file_location", fileName);
public static string GetFileLocation => AppSettings["file_location"];
public static void SetValue(string key, string value)
{
var appName = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
var configFile = Path.Combine(appName,
$"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.exe.config");
var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
var config = OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
throw new Exception($"Key {key} does not exist");
}
config.AppSettings.Settings[key].Value = value;
config.Save();
RefreshSection("appSettings");
}
}
}
控制台应用程序测试
namespace YourNamespace
{
class Program
{
static void Main(string[] args)
{
var current = ApplicationSettings.GetFileLocation;
Console.WriteLine($"Current path: {current}");
ApplicationSettings.SetFileLocation(current == "C:\\users\\test\\desktop\\file.csv" ?
"C:\\users\\test\\desktop\\file_location.csv" :
"C:\\users\\test\\desktop\\file.csv");
Console.WriteLine($"Current path after set: {ApplicationSettings.GetFileLocation}");
Console.ReadLine();
}
}
}
关于更新,请参阅以下
日本语で失礼します。
VisualStudio2022で同じ现象に遭遇しました。
この症状はVisualStudio2022でSettings.Designerが生成される时に余分な名前空间が追加されるようです。 自分の场合はターゲットムワークを.net3.5→4.8へ変更した时に発生しました。
Visual Studio 2019で生成されたSettings.Designer.vb
'
Option Strict On
Option Explicit On
<Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0"), _
Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Partial Friend NotInheritable Class MySettings
・
・
・
End Class
Namespace My
<Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
Friend Module MySettingsProperty
<Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")> _
Friend ReadOnly Property Settings() As Global.TestApp2019.MySettings
Get
Return Global.TestApp2019.MySettings.Default
End Get
End Property
End Module
End Namespace
Visual Studio 2022で生成されたSettings.Designer.vb
'
Option Strict On
Option Explicit On
Namespace My
<Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.9.0.0"), _
Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Partial Friend NotInheritable Class MySettings
・
・
・
End Class
End Namespace
Namespace My
<Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
Friend Module MySettingsProperty
<Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")> _
Friend ReadOnly Property Settings() As Global.TestApp2022.My.MySettings
Get
Return Global.TestApp2022.My.MySettings.Default
End Get
End Property
End Module
End Namespace
余计な命名空间Myを削除して.My.MySettingsをMySettingsに変更します。
'
Option Strict On
Option Explicit On
<Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.9.0.0"), _
Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Partial Friend NotInheritable Class MySettings
・
・
・
End Class
Namespace My
<Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
Friend Module MySettingsProperty
<Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")>
Friend ReadOnly Property Settings() As Global.TestApp2022.MySettings
Get
Return Global.TestApp2022.MySettings.Default
End Get
End Property
End Module
End Namespace
これで动作するようになりましたが、この不具合の発生原理が分かっていないため影响は分かりません。