【问题标题】:VB.NET Trying to read a System.Collections.Specialized.StringCollection in My.Settings returns an InvalidOperationExceptionVB.NET 尝试读取 My.Settings 中的 System.Collections.Specialized.StringCollection 返回 InvalidOperationException
【发布时间】:2013-06-17 08:27:19
【问题描述】:

在我的 vb.net 项目中,我使用 Visual Studio 的内置设置管理器创建了以下设置:

  • appVisible(布尔值)
  • saveFusedFiles(布尔值)
  • 颜色(System.Collections.Specialized.StringCollection)
  • 分隔符 (System.Collections.Specialized.StringCollection)

在尝试读取“颜色”或“分隔符”时——我得到一个 InvalidOperationException,但读取布尔变量有效。

在我的Config file 中找不到我的 System.Collections.Specialized.StringCollection 变量...

据我所知,设置变量的实例是自动创建的,所以这应该不是问题。

这是我读取设置的构造函数:

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    My.Settings.Reload()

    'INIT Separators

    If Not ListView_Separators.Items.Count = 0 Then
        ListView_Separators.Items.Clear()
    End If

    If My.Settings.separators.Count = 0 Then
        My.Settings.separators.Add(",")
        GenerateListViewItem(",")
    Else
        For Each seperator As String In My.Settings.separators
            GenerateListViewItem(seperator)
        Next
    End If
    Button_Add.Enabled = False
    Button_Delete.Enabled = False

    'INIT Colors

    If DataGridView_Colors.Rows.Count > 0 Then
        DataGridView_Colors.Rows.Clear()    'Clear DataGridView
    End If
    For Each color As String In My.Settings.colors  'Add all Colors to DataGridView
        'Add to DataGridView
        Dim splitedColor As String() = New String(1) {1, 1}
        splitedColor = color.Split("_")
        Dim contentText As String
        Select Case splitedColor(1)
            Case 0
                contentText = "New component"
            Case 1
                contentText = "Removed component"
            Case 2
                contentText = "Changed Data"
        End Select
        Dim arrDataGridRow As String() = New String(1) {splitedColor(0), contentText}
        DataGridView_Colors.Rows.Add(arrDataGridRow)
        Dim dgwStyle As New DataGridViewCellStyle
        dgwStyle.ForeColor = Drawing.Color.FromName(splitedColor(0))
        DataGridView_Colors.Rows(DataGridView_Colors.Rows.Count - 1).Cells(0).Style = dgwStyle
    Next

    'INIT View

    If My.Settings.appVisible = True Then
        Checkbox_Visable.Checked = True
    Else
        Checkbox_Visable.Checked = False
    End If

End Sub

My.Settings.separators.Count 抛出异常。

有人知道如何处理吗?

【问题讨论】:

  • 你能提供一些代码吗?
  • 当然,我在问题中添加了构造函数
  • 在运行时读取 My.Settings.separators.Count 时出现 InvalidOperationException
  • 我在您的配置文件中没有看到 colorsseparators

标签: vb.net invalidoperationexception my.settings stringcollection


【解决方案1】:

我认为这回答了为什么您没有在配置文件中看到该集合。

https://stackoverflow.com/a/23452534/3585500

但对同一问题的这个答案似乎是一个更好的解决方案。

https://stackoverflow.com/a/24112414/3585500

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多