开发者

How do I allow project user setting to be of type List<string>?

My app currently uses the user settings system (via project properties) to store user specific data. I use these settings instead of writing to/from App.Config because I need the data to persist when ClickOnce updates their build.

Right now I am needing the user to be able to specify a list of strings to be stored in user settings. I already deal with settings in a C# PropertyGrid, and I have verified that if I set MyPropertyGr开发者_C百科id.SelectedObject = new List<string>(), it gives me a proper editor to edit a list of strings.

Unfortunately, when I go to Project Preferences -> Settings and click Browse for the type, I cannot select List or find any good types to use for a string list. Does anyone have any suggestions for how to store a list of strings in the user settings?


You can add your own settings manually when the designer won't let you choose the types. This can happen with some built-in types and with a lot of your own custom types. Here's a link to a similar question on SO:

Why am I unable to select a custom Type for a setting from the same project/assembly as the settings file?

and here's a link to an answer (it's in VB.NET but the principles apply):

http://blog.coretech.dk/jgs/add-custom-data-type-structure-to-mysettings-in-vbnet-wpf/


Well... I can see System.Collections.Specialized.StringCollection in the list of types. Why don't you just use this?

You can use the following conversions between StringCollection and List<string>:

var stringList = stringCollection.Cast<string>().ToList();

var stringCollection = new StringCollection();
stringCollection.AddRange(stringList.ToArray());


You are not able to do it by the designer, but you can manual edit the settings file.

The thing is, that VS allows to serialize int[] type by default in the settings file - it just doesn't allow you to select it by default. So, create a setting with desired name (e.g. SomeTestSetting) and make it of any type (e.g. string by default). Save the changes.

Now go to your project folder and open the "Properties\Settings.settings" file with text editor (Notepad, for example) Or you can open it in VS by right-clicking in Solution Explorer on " -> Properties -> Settings.settings", select "Open With..." and then choose either "XML Editor" or "Source Code (Text) Editor". In the opened xml settings find your setting (it will look like this):

<Setting Name="SomeTestSetting" Type="System.String" Scope="User">
  <Value Profile="(Default)" />
</Setting>

Change the "Type" param from System.String to List. Now this section will look like this:

<Setting Name="SomeTestSetting" Type="System.Collections.Generic.List`1[System.String]" Scope="User">
  <Value Profile="(Default)" />
</Setting>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜