vb.net VB 2010 和 app.config 文件和配置文件已被另一个程序更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13672657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB 2010 and app.config file and configuration file has been changed by another program
提问by user1870837
I'm a beginner in Visual Studio, I'm dealing with app.config file. I just want to ask you a little tip: what is the best way to update a value key several times in app.config file using Windows Forms. So far I've tried this:
我是 Visual Studio 的初学者,我正在处理 app.config 文件。我只想问你一个小技巧:使用 Windows 窗体多次更新 app.config 文件中的值键的最佳方法是什么。到目前为止,我已经尝试过这个:
Just before that the Form1 is closed, I update a value with the next code:
就在 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)
Then the next form is open with:
然后打开下一个表单:
Form1.Hide()
Form2.Show()
But when I try to save again a value in the same key in the new Form2 it throws me an error an the program freezes:
但是当我尝试在新的 Form2 中再次保存相同键中的值时,它会抛出一个错误并且程序冻结:
The configuration file has been changed by another program.(C:\Users\RH\Documents\Visual Studio 2010\Projects\MyProyect\MyProyect\bin\Debug\MyProyect.exe.config)
配置文件已被另一个程序更改。(C:\Users\RH\Documents\Visual Studio 2010\Projects\MyProyect\MyProyect\bin\Debug\MyProyect.exe.config)
Really I've searching for a solution, but it seems that I'm the only one with this kind of problem. Like I'd say I'm just a beginner. Could you please give me an advice?
真的,我一直在寻找解决方案,但似乎我是唯一一个遇到这种问题的人。就像我会说我只是一个初学者。你能给我一个建议吗?
回答by Kratz
I think your problem is this, if you check the documentationfor the config.Savemethod, there is this statement,
我认为您的问题是这样的,如果您查看该方法的文档config.Save,则有此声明,
If the configuration file has changed since this Configuration object was created, a run-time error occurs.
如果自创建此 Configuration 对象后配置文件已更改,则会发生运行时错误。
Savechanges the file, so this leads me to believe that you can only call the save method once per instance of the Configurationobject. So, this leads me to believe that this,
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)
would fail on the second save, but then this,
会在第二次保存时失败,但是这个,
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)
Would succeed.
会成功。
回答by Maurice Reeves
Are you trying to save some user configurable value? In that case, the best case is to use a Settings file, which is similar to the app.config file, but is updatable during the application run. In all truth, the values you put in a *.settings file get inserted into the app.config file, but the process of reading and updating is managed by the application.
您是否想保存一些用户可配置的值?在这种情况下,最好的情况是使用设置文件,该文件类似于 app.config 文件,但在应用程序运行期间可更新。实际上,您放入 *.settings 文件中的值会插入到 app.config 文件中,但读取和更新过程由应用程序管理。
I have an application that allows users to read files from a folder, and I save the last folder location in the Settings file. The next time the application runs, I can read that value again for that specific user.
我有一个允许用户从文件夹中读取文件的应用程序,我将最后一个文件夹位置保存在设置文件中。下次应用程序运行时,我可以为该特定用户再次读取该值。
Here is some example code in C#:
以下是 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();
I translated it to VB.Net, but I apologize in advance as I haven't done VB.Net in a while, so you might want to sanity check it. :-D
我把它翻译成 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()

