How to load folder files in ListView?

I would like the user to select the folder with the FolderBrowserDialog and upload the files to the ListView.

My intention is to create a small playlist, so I need to change a couple of properties of the ListView control that I assume. What properties should I set in the control?

How can I achieve this?

+3
source share
3 answers

Of course, you just need to do the following:

    FolderBrowserDialog folderPicker = new FolderBrowserDialog();
    if (folderPicker.ShowDialog() == DialogResult.OK)
    {

        ListView1.Items.Clear();

        string[] files = Directory.GetFiles(folderPicker.SelectedPath);
        foreach (string file in files)
        {

            string fileName = Path.GetFileNameWithoutExtension(file);
            ListViewItem item = new ListViewItem(fileName);
            item.Tag = file;

            ListView1.Items.Add(item);

        }

    }

Then, to get the file again, follow these steps when you click a button or other event:

    if (ListView1.SelectedItems.Count > 0)
    {

        ListViewItem selected = ListView1.SelectedItems[0];
        string selectedFilePath = selected.Tag.ToString();

        PlayYourFile(selectedFilePath);

    }
    else
    {
        // Show a message
    }

For better viewing, set ListView mode to Details mode:

ListView1.View = View.Details;
+11
source

:

    public void DisplayFolder ( string folderPath )
    {
        string[ ] files = System.IO.Directory.GetFiles( folderPath );

        for ( int x = 0 ; x < files.Length ; x++ )
        {
            lvFiles.Items.Add( files[x]);
        }
    }
+2

private void buttonOK_Click_1 ( , EventArgs e)

    {

        DirectoryInfo FileNm = new DirectoryInfo(Application.StartupPath);
        var filename = FileNm.GetFiles("CONFIG_*.csv");

// CONFIG_123.csv, CONFIG_abc.csv ..

       foreach(FileInfo f in filename)
        listViewFileNames.Items.Add(f.ToString());

    }
0

Source: https://habr.com/ru/post/1722453/


All Articles