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);
aNode.Tag = subDir;
nodeToAddTo.Nodes.Add(aNode);
try
{
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
aNode.ImageIndex = 1;
aNode.SelectedImageIndex = 2;
GetDirectories(subSubDirs, aNode);
aNode.Tag = subDir;
}
}
catch (UnauthorizedAccessException)
{
aNode.ImageIndex = 3;
aNode.SelectedImageIndex = 3;
}
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!");
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);
if (File.Exists(filepath))
{
MessageBox.Show("aaaaa");
}
else
{
MessageBox.Show("File does not exist anymore!");
}
}