开发者

vb.NET trying to store an Array in My.Settings

I want to store a regular array in My.Settings. Not an ArrayList, not a StringCollection, just a string Array. Can this be done? If so, how is it done? I c开发者_运维问答an't seem to find the string array datatype in the settings designer in Visual Studio.


Update: Take a look at the Advanced Settings section in the MSDN article Using My.Settings in Visual Basic 2005. It looks like the approach outlined in that section (subclassing ApplicationSettingsBase and providing your own properties) might be what you're looking for.


You can't create a setting whose type is String(), presumably because then the settings infrastructure would need to support any array T(), which wouldn't work for most types.

I personally think it would make sense to just use a StringCollection and then populate an array with that in code:

Module SettingsExtensions

    Public Function GetMySettingArray() As String()
        Dim mySetting As StringCollection = My.Settings.MySetting

        ' If you're using .NET 3.5 or greater:
        Return mySetting.Cast(Of String)().ToArray()

        ' Otherwise:
        Dim array(mySetting.Count - 1) As String
        mySetting.CopyTo(array, 0)

        Return array
    End Function

End Module


You cannot use an Array directly in My.Settings. You can probably define a class with a property which is an array of String, but why not use StringCollection instead? Whatever you choose to do, remember that whatever you put in My.Settings must be XML serializable, because the settings are saved in XML format.


I don't think you can store a regular array in My.Settings. My.Settings is intended for just "settings", such as color, image, etc.

If you have a large array of strings I guess I would store them in a text file, an XML file, or in a SQL server database.

If it is a really short list of strings... I suppose you could store it in my.settings as a single string with a unique character separating each original string.

When you read it back in use the Split() method to break it back up into the original string list.

If you really wanted to store an array of strings in My.Settings, the easist way would probably be to use "StringCollection" for the setting type

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜