我是Visual Studio的初学者,我正在处理app.config文件。 我只想问你一个小提示:使用Windows Forms在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)
我真的在寻找解决方案,但似乎我是唯一一个有这种问题的人。就像我说我只是一个初学者。你能给我一个建议吗?
答案 0 :(得分:1)
我认为您的问题是这样,如果您检查config.Save
方法的documentation,则会出现此声明,
如果自此Configuration对象后配置文件已更改 创建时,发生运行时错误。
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)
会成功。
答案 1 :(得分:1)
您是否尝试保存一些用户可配置的值?在这种情况下,最好的情况是使用一个与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()