I have a list of information in a string dictionary that I want to store in the app.config for my program.
我有一个字符串字典中的信息列表,我想存储在我的程序的app.config中。
I've created the entry in the "settings" portion of the program called "extentions" but when I go to use "Settings.Default.Extentions" I get an error that it is null.
我在程序的“设置”部分创建了一个名为“extentions”的条目,但是当我使用“Settings.Default.Extentions”时,我收到一个错误,它是null。
Is there a trick or something to using this?
是否有使用此技巧或其他东西?
private void LoadextList()
{
listBox1.Items.Clear();
foreach (KeyValuePair<string, string> kvp in Settings.Default.Extentions)
{
listBox1.Items.Add(kvp.Key + "\t" + kvp.Value);
}
}
3 个解决方案
#1
I don't understand your Extensions problem, but I've used this code before to convert between a string and NameValueCollection - you can easily change it to use a StringDictionary:
我不明白你的Extensions问题,但我之前使用过这段代码来转换字符串和NameValueCollection - 你可以很容易地将它改为使用StringDictionary:
public NameValueCollection StringToNameValueCollection(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
{
NameValueCollection nvc = new NameValueCollection();
// split string into array of key/value pairs
string[] kvpairs = KeyValueData.Split(new char[]{ItemSeparator});
// add each pair to the collection
for (int i = 0; i < kvpairs.Length; i++)
{
if (!string.IsNullOrEmpty(kvpairs[i]))
{
if (kvpairs[i].Contains(KeyValueSeparator.ToString()))
{
// get the key
string k = kvpairs[i].Remove(kvpairs[i].IndexOf(KeyValueSeparator.ToString())).Trim();
// get the value
string v = kvpairs[i].Remove(0, kvpairs[i].IndexOf(KeyValueSeparator) + 1).Trim();
// add key/value if key is valid
if (!string.IsNullOrEmpty(k))
{
nvc.Add(k, v);
}
}
else
{
// no key/value separator in the pair, so add whole string as key and value
nvc.Add(kvpairs[i].Trim(), kvpairs[i].Trim());
}
}
}
return nvc;
}
Convert NameValueCollection to string:
将NameValueCollection转换为字符串:
public string NameValueCollectionToString(NameValueCollection nvc, char KeyValueSeparator, char ItemSeparator)
{
StringBuilder sb = new StringBuilder();
for(int i = 0; i < nvc.Count; i++)
{
if (i != 0)
{
sb.Append(ItemSeparator);
}
sb.Append(nvc.Keys[i]);
sb.Append(KeyValueSeparator);
sb.Append(nvc[i]);
}
return sb.ToString();
}
I've had to make a couple of changes from the code I used, but it should build. Note that it's not too forgiving - the separator characters cannot appear in either the 'key' or 'value' strings.
我必须从我使用的代码中进行一些更改,但它应该构建。请注意,它不太宽容 - 分隔符不能出现在'key'或'value'字符串中。
#2
The following question probably contains useful information:
以下问题可能包含有用的信息:
How to store a HashTable in the usersettings?
如何在用户设置中存储HashTable?
The problem you are facing is that the two serialization options available when you use a Settings.settings file and the associated auto-generated class are serialization to XML or serialization to string.
您遇到的问题是,当您使用Settings.settings文件和关联的自动生成的类时,可用的两个序列化选项是序列化为XML或序列化为字符串。
The StringDictionary
class does not support neither XML nor string serialization. There are some ways you can workaround this limitation and a few of them are listed on the answers to previously mentioned question.
StringDictionary类既不支持XML也不支持字符串序列化。有一些方法可以解决这个限制,其中一些列在前面提到的问题的答案中。
#3
public Dictionary<string,string> StringToDictonary(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
{
Dictionary<string, string> Dic = new Dictionary<string, string>();
string[] cells = KeyValueData.Split(ItemSeparator);
foreach (string cell in cells)
{
if (!string.IsNullOrEmpty(cell))
{
string[] value = cell.Split(KeyValueSeparator);
if (!string.IsNullOrEmpty(value[0]) && !string.IsNullOrEmpty(value[1]))
{
Dic.Add(value[0], value[1]);
}
}
}
return Dic;
}
break
public string dictonaryToString(Dictionary<string, string> nvc, char KeyValueSeparator, char ItemSeparator)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (KeyValuePair<string,string> kvp in nvc)
{
if (!first)
{
sb.Append(ItemSeparator);
first = false;
}
sb.Append(kvp.Key);
sb.Append(KeyValueSeparator);
sb.Append(kvp.Value);
}
return sb.ToString();
}
}
#1
I don't understand your Extensions problem, but I've used this code before to convert between a string and NameValueCollection - you can easily change it to use a StringDictionary:
我不明白你的Extensions问题,但我之前使用过这段代码来转换字符串和NameValueCollection - 你可以很容易地将它改为使用StringDictionary:
public NameValueCollection StringToNameValueCollection(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
{
NameValueCollection nvc = new NameValueCollection();
// split string into array of key/value pairs
string[] kvpairs = KeyValueData.Split(new char[]{ItemSeparator});
// add each pair to the collection
for (int i = 0; i < kvpairs.Length; i++)
{
if (!string.IsNullOrEmpty(kvpairs[i]))
{
if (kvpairs[i].Contains(KeyValueSeparator.ToString()))
{
// get the key
string k = kvpairs[i].Remove(kvpairs[i].IndexOf(KeyValueSeparator.ToString())).Trim();
// get the value
string v = kvpairs[i].Remove(0, kvpairs[i].IndexOf(KeyValueSeparator) + 1).Trim();
// add key/value if key is valid
if (!string.IsNullOrEmpty(k))
{
nvc.Add(k, v);
}
}
else
{
// no key/value separator in the pair, so add whole string as key and value
nvc.Add(kvpairs[i].Trim(), kvpairs[i].Trim());
}
}
}
return nvc;
}
Convert NameValueCollection to string:
将NameValueCollection转换为字符串:
public string NameValueCollectionToString(NameValueCollection nvc, char KeyValueSeparator, char ItemSeparator)
{
StringBuilder sb = new StringBuilder();
for(int i = 0; i < nvc.Count; i++)
{
if (i != 0)
{
sb.Append(ItemSeparator);
}
sb.Append(nvc.Keys[i]);
sb.Append(KeyValueSeparator);
sb.Append(nvc[i]);
}
return sb.ToString();
}
I've had to make a couple of changes from the code I used, but it should build. Note that it's not too forgiving - the separator characters cannot appear in either the 'key' or 'value' strings.
我必须从我使用的代码中进行一些更改,但它应该构建。请注意,它不太宽容 - 分隔符不能出现在'key'或'value'字符串中。
#2
The following question probably contains useful information:
以下问题可能包含有用的信息:
How to store a HashTable in the usersettings?
如何在用户设置中存储HashTable?
The problem you are facing is that the two serialization options available when you use a Settings.settings file and the associated auto-generated class are serialization to XML or serialization to string.
您遇到的问题是,当您使用Settings.settings文件和关联的自动生成的类时,可用的两个序列化选项是序列化为XML或序列化为字符串。
The StringDictionary
class does not support neither XML nor string serialization. There are some ways you can workaround this limitation and a few of them are listed on the answers to previously mentioned question.
StringDictionary类既不支持XML也不支持字符串序列化。有一些方法可以解决这个限制,其中一些列在前面提到的问题的答案中。
#3
public Dictionary<string,string> StringToDictonary(string KeyValueData, char KeyValueSeparator, char ItemSeparator)
{
Dictionary<string, string> Dic = new Dictionary<string, string>();
string[] cells = KeyValueData.Split(ItemSeparator);
foreach (string cell in cells)
{
if (!string.IsNullOrEmpty(cell))
{
string[] value = cell.Split(KeyValueSeparator);
if (!string.IsNullOrEmpty(value[0]) && !string.IsNullOrEmpty(value[1]))
{
Dic.Add(value[0], value[1]);
}
}
}
return Dic;
}
break
public string dictonaryToString(Dictionary<string, string> nvc, char KeyValueSeparator, char ItemSeparator)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (KeyValuePair<string,string> kvp in nvc)
{
if (!first)
{
sb.Append(ItemSeparator);
first = false;
}
sb.Append(kvp.Key);
sb.Append(KeyValueSeparator);
sb.Append(kvp.Value);
}
return sb.ToString();
}
}