I have a question about C#'s App.config.
When I launch the app, I would like to read the data in drop-down list 1 and other drop-down list 2 from App.config.Also, I will use the key and its value later, so I haven't implemented it yet, but when I read it from App.config, I'm thinking of putting it in an array or a list.
By adding data to App.config, users want to be able to increase the list of drop-down lists and other drop-down lists without having to tamper with the program.
Currently, I'm thinking of doing a program called loading everything in foreach, but
The question is how to separate the data to be used in drop-down list 1 from the data to be used in drop-down list 2.
Thank you for your cooperation.
class1 ↓
private void Form1_Load (object sender, EventArgse)
{
// Get all keys and their values (actually, I want to separate comboBox1 and 2)
foreach (string key in System.Configuration.ConfigurationSettings.AppSettings.AllKeys)
{
// key = a, b is comboBox1
comboBox1.Items.Add (System.Configuration.ConfigurationSettings.AppSettings[key]);
// Not implemented to store in array
// key = AB, CD is comboBox2
comboBox2.Items.Add (System.Configuration.ConfigurationSettings.AppSettings [key]);
// Not implemented to store in array
}
}
App.config↓
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0"sku=".NETFramework, Version=v4.5.2"/>
</startup>
<appSettings>
// List 1
<add key="a" value="1"/>
<add key="b" value="2"/>
// Users add more and more here
// List 2
<add key="AB" value="3"/>
<add key="CD" value="4"/>
// Users add more and more here
</appSettings>
</configuration>
Basically, as pgrho said, Visual Studio has some useful features, so I'll introduce them to you.
First, it is easy to use the System.Collections.Specialized.StringCollection
in Settings.settings without having to create the Entry
class.App.config created by this is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<WindowsFormsApp1.Properties.Settings>
<setting name="Key1" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>1</string>
<string>2</string>
</ArrayOfString>
</value>
</setting>
</WindowsFormsApp1.Properties.Settings>
</userSettings>
</configuration>
(string
instead of Entry
).
You can also use the DataSource
property to set this collection to ComboBox
.
comboBox1.DataSource=Settings.Default.Key1;
However, this method cannot be used to add items that are not listed in App.config, and you must add one item at a time.
The policy is
and so on.
For example, change the name of the key to List1_a
or List2_AB
and add conditions such as key.StartsWith("List1_")
.Obtain the key name with key.Substring(6)
.
2. For example, the value is
<add key="list1" value="a:1;b:2"/>
<add key="list2" value="AB:3;CD:4"/>
Configure a string containing all items, as shown in .In the above example, ;
and :
are separated, but considering the control characters and the trouble of parsing, it is easy to store them in XML
or JSON
.
3. Use the items generated by Settings.settings
instead of the <appSettings>
section.
First, open "Settings" from the project properties and register two appropriate key names.
Next, you need to specify a type, but I don't think the default type will work, so prepare a separate appropriate "class library" project, for example:
public structure entry
{
[XmlAttribute]
public string key {get;set;}
XmlText
public string value {get;set;}
}
public sold class EntryCollection—Collection <Entry >
{
}
If you build this project and view it from a project that contains Settings.settings
, you can select the above type from Browse in Settings.
Now that I've prepared so far, I'm going to start with the C# code.
EntryCollection entries=Settings.Default.key1;
if(entries!=null)
{
foreach (entry entries)
{
comboBox1.Items.Add(e.Value);
}
}
You can refer to the app.config
setting as either userSettings
or applicationSettings
.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<Project Name.Properties.Settings>
<setting name="key 1" serializeAs="Xml">
<value>
<ArrayOfEntry>
<Entry Key="a">1</Entry>
<Entry Key="b">2</Entry>
</ArrayOfEntry>
</value>
</setting>
<setting name="key 2" serializeAs="Xml">
<value>
<ArrayOfEntry>
<Entry Key="AB">3</Entry>
<Entry Key="CD">4</Entry>
</ArrayOfEntry>
</value>
</setting>
</Project Name.Properties.Settings>
</userSettings>
</configuration>
appears in
I do not recommend using multiple keys of <appSettings>
because there may be unexpected keys.I think the method below is safer and more reliable.
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.