Click here to Skip to main content
15,306,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all, i created a app using c# winform it has a tree view and listview.

if i clicked some folder in tree view the files from that folder will reflect on the listview, i want to get the file path of those files.

The link below is my whole project
https://drive.google.com/file/d/1tmlH55T8oEmR7ogWn2ld_LpkVxRrShPq/view?usp=sharing

Below is my code in the project below.

private void PopulateTreeView(string Drive)
        {
            DirectoryInfo info = new DirectoryInfo(Drive);
            if (info.Exists)
            {
                TreeNode rootNode = new TreeNode(info.Name, 0, 0);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Add(rootNode);
            }
        }


private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                aNode = new TreeNode(subDir.Name, 1, 2); //1 and 2 are the icons in the image list!
                aNode.Tag = subDir;
                nodeToAddTo.Nodes.Add(aNode);
                try
                {
                    subSubDirs = subDir.GetDirectories();
                    if (subSubDirs.Length != 0)
                    {
                        aNode.ImageIndex = 1; //folder icons in the image list!
                        aNode.SelectedImageIndex = 2; //folder icons in the image list!
                        GetDirectories(subSubDirs, aNode);
                        aNode.Tag = subDir;
                    }
                }         
                catch (UnauthorizedAccessException)
                {
                    aNode.ImageIndex = 3; //folder icons in the image list!
                    aNode.SelectedImageIndex = 3; //folder icons in the image list!
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "DirectoryLister", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode newSelected = e.Node;
            listView1.Items.Clear();
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;
            try
            {
                foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
                {
                    item = new ListViewItem(dir.Name, 1);
                    subItems = new ListViewItem.ListViewSubItem[]
                    {
                    new ListViewItem.ListViewSubItem(item, "Directory"),new ListViewItem.ListViewSubItem(item,dir.LastAccessTime.ToShortDateString())
                    };
                    item.SubItems.AddRange(subItems);
                    listView1.Items.Add(item);
                }
                foreach (FileInfo file in nodeDirInfo.GetFiles())
                {
                    item = new ListViewItem(file.Name, 4);
                    subItems = new ListViewItem.ListViewSubItem[]
                    {
                    new ListViewItem.ListViewSubItem(item, "File"),new ListViewItem.ListViewSubItem(item,file.LastAccessTime.ToShortDateString())
                    };
                    item.SubItems.AddRange(subItems);
                    listView1.Items.Add(item);
                }
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You have no access to this folder!"); // this will pop up if you have no access in the folder!
                return;
            }
        }


What I have tried:

EDIT:

I tried the code below, it works but the file path is wrong it keeps throwing a directory path from the application current path, it can't get the file path of the selected item in the list view.

if (listView1.SelectedItems.Count == 0) return;
            for (int i = 0; i <= listView1.SelectedItems.Count - 1; i++)
            {
                string extension = Path.GetExtension(listView1.SelectedItems[i].Text);               
                string filepath = Path.GetFullPath(listView1.SelectedItems[i].Text);
                //string filepath = "";
                if (File.Exists(filepath))
                {
                    MessageBox.Show("aaaaa");
                }
                else
                {
                    MessageBox.Show("File does not exist anymore!");
                }
            }
Posted
Updated 14-Mar-21 16:35pm
v2
Comments
Maciej Los 15-Mar-21 5:22am
   
Check what value returns this line: listView1.SelectedItems[i].Text...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900