Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在app.config中存储和检索StringDictionary?_C#_App Config_Stringdictionary - Fatal编程技术网

C# 如何在app.config中存储和检索StringDictionary?

C# 如何在app.config中存储和检索StringDictionary?,c#,app-config,stringdictionary,C#,App Config,Stringdictionary,我有一个字符串字典中的信息列表,我想将其存储在app.config中以供我的程序使用 我已经在程序的“设置”部分创建了名为“扩展”的条目,但是当我使用“settings.Default.extensions”时,我得到一个错误,它为空 使用这个有什么诀窍吗 private void LoadextList() { listBox1.Items.Clear(); foreach (KeyValuePair<string, string> kvp in Settings.

我有一个字符串字典中的信息列表,我想将其存储在app.config中以供我的程序使用

我已经在程序的“设置”部分创建了名为“扩展”的条目,但是当我使用“settings.Default.extensions”时,我得到一个错误,它为空

使用这个有什么诀窍吗

private void LoadextList()
{
    listBox1.Items.Clear();
    foreach (KeyValuePair<string, string> kvp in Settings.Default.Extentions)
    {
        listBox1.Items.Add(kvp.Key + "\t" + kvp.Value);
    }
}
private void LoadextList()
{
listBox1.Items.Clear();
foreach(Settings.Default.extensions中的KeyValuePair kvp)
{
listBox1.Items.Add(kvp.Key+“\t”+kvp.Value);
}
}

我不理解您的扩展问题,但我以前使用过此代码在字符串和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;
    }
publicNameValueCollection StringtonNameValueCollection(字符串KeyValueData、字符KeyValueSeparator、字符ItemSeparator)
{
NameValueCollection nvc=新的NameValueCollection();
//将字符串拆分为键/值对数组
string[]kvpairs=KeyValueData.Split(新字符[]{itemselector});
//将每对添加到集合中
for(int i=0;i
将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();
    }
公共字符串NameValueCollectionToString(NameValueCollection nvc、char-KeyValueSeparator、char-ItemSeparator)
{
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
我不得不对我使用的代码做一些更改,但它应该可以构建。请注意,它不太宽容-分隔符字符不能出现在“键”或“值”字符串中。

公共字典字符串说明(字符串KeyValueData、char KeyValueSeparator、char ItemSeparator)
  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;
}
{ Dictionary Dic=新字典(); string[]cells=KeyValueData.Split(ItemSeparator); foreach(单元格中的字符串单元格) { 如果(!string.IsNullOrEmpty(单元格)) { 字符串[]值=cell.Split(KeyValueSeparator); 如果(!string.IsNullOrEmpty(值[0])&&!string.IsNullOrEmpty(值[1])) { 添加(值[0],值[1]); } } } 返回Dic; }
中断

公共字符串dictionarytostring(字典nvc、char-KeyValueSeparator、char-itemselector)
{
StringBuilder sb=新的StringBuilder();
bool first=true;
foreach(nvc中的KeyValuePair kvp)
{
如果(!第一个)
{
某人附加(项目分隔符);
第一个=假;
}
sb.Append(kvp.Key);
sb.Append(键值分隔符);
sb.追加(kvp.值);
}
使某人返回字符串();
}
}

以下问题可能包含有用的信息:

您面临的问题是,在使用Settings.Settings文件和关联的自动生成类时,有两个序列化选项可用:序列化为XML或序列化为字符串


StringDictionary
类既不支持XML也不支持字符串序列化。有一些方法可以绕过此限制,其中一些方法已在前面提到的问题的答案中列出。

您可以将创建扩展的代码/屏幕截图粘贴到创建扩展的位置吗?我在“设置”下的项目属性中创建了扩展,并将其作为collections.special.StringDictional(如果你真的想要的话,我可以发表一篇屏幕上的长篇大论)我还应该提到,我可以在app.config中使用字符串来实现这一点。我只是不知道如何使用字符串字典。看看这个优雅的解决方案,它可能会让我绕过这个问题。我会尝试一下,我仍然觉得我缺少一些基本的东西,不知道为什么我不能保存字符串。我现在正在整理它,但我不知道我们的第一个方法抛出了一个错误,即并非所有路径都会返回(我可能会找到答案,但您可能需要为大众编辑代码)谢谢,看起来好像遗漏了一个大括号,现在应该可以了。
    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();
    }
}