3

我是 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)

真的我一直在寻找解决方案,但似乎我是唯一一个遇到这种问题的人。就像我会说我只是一个初学者。你能给我一个建议吗?

4

2 回答 2

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()
于 2012-12-06T20:04:50.903 回答
1

我认为您的问题是这样的,如果您检查该方法的文档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)

会成功的。

于 2012-12-06T19:43:39.047 回答