简体   繁体   中英

How can I display a file selected from directory listview control with c# window forms?

My code below displays the listview of a folder, which is selected by the folderBrowser control. After displaying the folders and files in a list view, I want to open the file which is clicked. I have written the following code using LISTVIEW_itemactivation . The problem is that I can't open the selected file. What am I doing wrong?

private void PopulateListView()
{
    listView1.Clear();
    //headers listview

    listView1.Columns.Add("File Name", 150);
    listView1.Columns.Add("Last Accessed", 110);
    listView1.Columns.Add("Size", 100);
    //listView1.ItemActivate += new System.EventHandler(listView1_ItemActivate);


    if (folderBrowser.ShowDialog() == DialogResult.OK)
    {

        string[] files = Directory.GetFiles(folderBrowser.SelectedPath);
        string[] folders = Directory.GetDirectories(folderBrowser.SelectedPath);
        foreach (string file in files)
        {
            long folderSize = 0;
            string fileName = Path.GetFileNameWithoutExtension(file);

            FileInfo finfo = new FileInfo(file);
            folderSize += finfo.Length;

            ListViewItem item = new ListViewItem(new[] { fileName, File.GetLastAccessTime(file).ToString(), finfo.Length.ToString() });

            images();
            item.ImageIndex = 1;
            listView1.Items.Add(item);

        }

        foreach (string file in folders)
        {
            string fileName = Path.GetFileNameWithoutExtension(file);
            ListViewItem item = new ListViewItem(new[] { fileName, File.GetLastAccessTime(file).ToString(), file.Length.ToString() });
            images();
            item.ImageIndex = 0;
            listView1.Items.Add(item);
        }
    }
}
private void button1_Click(object sender, EventArgs e)
{
    PopulateListView();
    textBox1.Text = folderBrowser.SelectedPath;
}
private void images(){
    try
    {
        imageList1.Images.Add(Bitmap.FromFile("./images/file.gif"));
        imageList1.Images.Add(Bitmap.FromFile("./images/Folder.gif"));
    }
    catch (FileNotFoundException) { }

}


private void listView1_DoubleClick(object sender, EventArgs e)
{

        ListViewItem item_clicked = listView1.SelectedItems[0];

}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    listView1.View = View.LargeIcon;
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    listView1.View = View.SmallIcon;
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
    listView1.View = View.Details;
}

private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
    listView1.View = View.List;
}

private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
    listView1.View = View.Tile;
}

private void listView1_ItemActivate(object sender, EventArgs e)
{

        try
        {
    string sPath = listView1.SelectedItems.ToString();
    string sFileName = listView1.FocusedItem.Text;

    Process.Start(sPath + "\\" + sFileName);
}
catch(Exception Exc)    {   MessageBox.Show(Exc.ToString());    }    
}

Well, based on how you're populating the list, this modified code should work.

private void listView1_ItemActivate(object sender, EventArgs e)
{
    try
    {
        string sPath = listView1.SelectedItem.SubItems[0].Text;
        Process.Start(sPath);
    }
    catch(Exception Exc)
    {
        MessageBox.Show(Exc.ToString());
    }
}

But you'll also need to get rid of this line because you'll never be able to load the file without knowing it's extension.

string fileName = Path.GetFileNameWithoutExtension(file);

See, when you load a ListViewItem like you are it's loading all of the SubItems with that array. Further, the array returned by Directory.GetFiles(folderBrowser.SelectedPath); returns an array of files with a full path to them, so, if you pick up the first SubItem that's going to be the full path to that file and thus issuing Process.Start on that file path will cause the shell to open the file in its default program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM