Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB 2010 and app.config file and configuration file has been changed by another program

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:

Just before that the Form1 is closed, I update a value with the next code:

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:

The configuration file has been changed by another program.(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?

like image 560
user1870837 Avatar asked Dec 14 '25 10:12

user1870837


2 Answers

I think your problem is this, if you check the documentation for the config.Save method, there is this statement,

If the configuration file has changed since this Configuration object was created, a run-time error occurs.

Save changes the file, so this leads me to believe that you can only call the save method once per instance of the Configuration object. So, this leads me to believe that 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)
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.

like image 82
Kratz Avatar answered Dec 16 '25 18:12

Kratz


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.

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#:

//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

'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()
like image 43
Maurice Reeves Avatar answered Dec 16 '25 19:12

Maurice Reeves