我是Visual Studio的初学者,我正在处理app.config文件。我只想问你一个小提示:使用 Windows 窗体在 app.config 文件中多次更新值键的最佳方法是什么。到目前为止,我已经尝试过这个:
在关闭 Form1 之前,我使用下一个代码更新一个值:
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)
然后打开下一个表单,其中包含:
Form1.Hide()
Form2.Show()
但是当我尝试在新 Form2 中的同一键中再次保存一个值时,它会给我一个错误,程序冻结:
配置文件已被另一个程序更改。(C:\Users\RH\Documents\Visual Studio 2010\Projects\MyProyect\MyProyect\bin\Debug\MyProyect.exe.config)
真的,我一直在寻找解决方案,但似乎我是唯一一个遇到这种问题的人。就像我会说我只是一个初学者。你能给我一个建议吗?
我认为你的问题是这样的,如果你检查config.Save
方法的文档,有这样的说法,
如果配置文件自此配置对象以来已更改 创建时,发生运行时错误。
Save
更改了文件,因此这让我相信您只能为每个Configuration
对象的实例调用一次 save 方法。所以,这让我相信,
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)
aps.Settings.Item("SomeKey").Value = 15 'just an example
config.Save(ConfigurationSaveMode.Modified)
将在第二次保存时失败,但随后,
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "MyProyect.exe")
Dim aps As AppSettingsSection = config.AppSettings
aps.Settings.Item("SomeKey").Value = 5 'just an example
config.Save(ConfigurationSaveMode.Modified)
'reopen
config = ConfigurationManager.OpenExeConfiguration(Application.StartupPath & "MyProyect.exe")
aps = config.AppSettings
aps.Settings.Item("SomeKey").Value = 15 'just an example
config.Save(ConfigurationSaveMode.Modified)
会成功。
您是否正在尝试保存一些用户可配置的值? 在这种情况下,最好的情况是使用设置文件,该文件类似于 app.config 文件,但在应用程序运行期间可更新。 事实上,您在 *.settings 文件中输入的值将插入到 app.config 文件中,但读取和更新过程由应用程序管理。
我有一个允许用户从文件夹中读取文件的应用程序,我将最后一个文件夹位置保存在设置文件中。 下次应用程序运行时,我可以再次读取该特定用户的该值。
下面是 C# 中的一些示例代码:
//read the property on load
if (Properties.Settings.Default["FileLocation"] != null
&& !string.IsNullOrWhiteSpace(Properties.Settings.Default["FileLocation"].ToString()))
{
DirectoryInfo dirInfo
= new DirectoryInfo(Properties.Settings.Default["FileLocation"].ToString());
if (dirInfo.Exists)
dlg.InitialDirectory = dirInfo.FullName;
}
//code removed for clarity
//....
//save on exit from method
FileInfo fi = new FileInfo(dlg.FileName);
Properties.Settings.Default["FileLocation"] = fi.DirectoryName;
Properties.Settings.Default.Save();
我把它翻译成 VB.Net,但我提前道歉,因为我有一段时间没有做 VB.Net 了,所以你可能想检查一下。 :-D
'read the property on load
If (Properties.Settings.Default["FileLocation"] IsNot Nothing _
AndAlso Not string.IsNullOrWhiteSpace(Properties.Settings.Default["FileLocation"].ToString())) Then
Dim dirInfo as DirectoryInfo _
= new DirectoryInfo(Properties.Settings.Default["FileLocation"].ToString())
if (dirInfo.Exists) Then
dlg.InitialDirectory = dirInfo.FullName
End If
End If
'code removed for clarity
'....
'save on exit from method
Dim fi as FileInfo = new FileInfo(dlg.FileName)
Properties.Settings.Default["FileLocation"] = fi.DirectoryName
Properties.Settings.Default.Save()