【问题标题】:VB 2010 and app.config file and configuration file has been changed by another programVB 2010 和 app.config 文件和配置文件已被另一个程序更改
【发布时间】:2012-11-20 07:02:39
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net app-config


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      我认为你的问题是这样的,如果你检查documentationconfig.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)
      

      会成功的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多