为什么我的列表< string>不救? [英] Why my list<string> doesn´t save?

查看:92
本文介绍了为什么我的列表< string>不救?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我有一个清单< string>保存在我的应用程序用户设置中,此列表应该保存openfiledialog的文件路径然后反序列化它,但我已经调用方法Save();甚至方法Upgrade();将字符串添加到列表后,但此字符串不会保存。为什么?



我尝试过:



保存它:

private void ImportIcon_Click(object sender,EventArgs e)

{

IfSound();

openFileDialog1.Filter =Analytica Files(* .analy)| * .analy;

openFileDialog1.Multiselect = true;

if(openFileDialog1.ShowDialog()== DialogResult.OK)

{

var Path = openFileDialog1.FileName;

foreach(openFileDialog1.FileNames中的字符串文件)

{

Settings.Default.FileList.Add(file);

Settings.Default.Save();

//Settings.Default.Upgrade ();

}

Good Day, I Have A List<string> Saved In My App User Settings, this List is supposed to save the file paths of a openfiledialog to then deserialize it, but I already Called the method Save(); And even the method Upgrade(); After adding the strings to the list, but this strings doesn´t save. Why?

What I have tried:

To Save It:
private void ImportIcon_Click(object sender, EventArgs e)
{
IfSound();
openFileDialog1.Filter = "Analytica Files (*.analy) |*.analy";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var Path = openFileDialog1.FileName;
foreach (string file in openFileDialog1.FileNames)
{
Settings.Default.FileList.Add(file);
Settings.Default.Save();
//Settings.Default.Upgrade();
}

推荐答案

检查设置类型。没有自定义处理程序,无法更新应用程序设置 - 参考; 应用程序设置概述| Microsoft Docs [ ^ ]

使用调试器,如果设置已定义但未初始化,则为null&你无法为它添加值 - 打击代码在测试应用程序中工作 - 值通过重新启动应用程序 - 创建一个名为 TestList 的用户设置进行测试;

Check the Setting Type. Application Settings cannot be updated without a custom handler - refer; Application Settings Overview | Microsoft Docs[^]
Use your debugger, if the setting is defined but not initialised, it will be null & you cannot add values to it - the blow code works in a Test application - the values are carried over restarts of the application - create a User Setting named TestList to test;
if(Properties.Settings.Default.TestList == null)
    {
        MessageBox.Show("Not initialised");
    // Create a new List and Save to Settings        
    Properties.Settings.Default.TestList = new System.Collections.Specialized.StringCollection();
        Properties.Settings.Default.Save();
    }
    else if(Properties.Settings.Default.TestList.Count > 0)
    {
        // Create a message showing the last value in the list and the count of items
        string strMsg = string.Format("List is populated\r\nLast Value: {0}\r\nCount: {1}", Properties.Settings.Default.TestList[Properties.Settings.Default.TestList.Count - 1].ToString(), Properties.Settings.Default.TestList.Count.ToString());
           
    MessageBox.Show(strMsg);
    }
    else
    {
        MessageBox.Show("Currently empty");
    }

    string[] myTest = new string[] { "Value 1", "Value 2" };
    
    foreach(string myVal in myTest)
    {
        Properties.Settings.Default.TestList.Add(myVal);
    }
    Properties.Settings.Default.Save();

    if (Properties.Settings.Default.TestList.Count > 0)
    {
        MessageBox.Show("After populate\r\n" + Properties.Settings.Default.TestList[0].ToString()
            + "\r\nCount: " + Properties.Settings.Default.TestList.Count);
    }





亲切的问候



Kind Regards


这篇关于为什么我的列表&lt; string&gt;不救?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆