在现有列表框和数组中添加新项目

时间:2019-02-13 21:58:47

标签: c# arrays winforms listbox

我有用作列表框的代码,用作播放列表。我想提供在通过新按钮加载第一首歌曲后添加新歌曲的选项。我该怎么办?

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() ==  System.Windows.Forms.DialogResult.OK)
            {
                files = openFileDialog1.SafeFileNames; //save only the names
                paths = openFileDialog1.FileNames; // save the paths
                for (int i = 0; i < files.Length; i++)
                {
                    listBox1.Items.Add(files[i]); // add songs to listbox


                }
             }

1 个答案:

答案 0 :(得分:0)

如果要在关闭并打开应用程序后使用音频文件,则也必须处理文件路径。创建一个文件列表文件,并在Load事件中将其打开。下面的代码会将文件添加到播放列表中。

        OpenFileDialog OpenFile = new OpenFileDialog();
        OpenFile.Filter = "Audio files (*.mp3)|*.mp3";
        OpenFile.Multiselect = true;

        System.IO.StreamWriter settingsFile = new System.IO.StreamWriter((Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + "playlist.ini");

        if (OpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string filename in OpenFile.FileNames)
            {                 
                listBoxList.Items.Add(filename);
                settingsFile.WriteLine(filename);
            }
        }

        settingsFile.Close();