我的程序有问题。我想创建一个类似Window Explore的应用程序,当我双击ListView中的项目时,它将显示文件/文件夹部分。我使用此代码Path.GetFullPath(fileName)
来查找所选项目名称的完整路径,但它始终返回我当前的项目路径。我无论如何都找不到解决它。有谁知道请帮助我!非常感谢!这是我的代码:
private void listViewFolder_DoubleClick(object sender, EventArgs e)
{
if (this.listViewFolder.SelectedItems.Count == 1)
{
ListView temp = (ListView)sender;
string fileName = ((ListViewItem)temp.SelectedItems[0]).Text;
string filePath = Path.GetFullPath(fileName);
this.listViewFolder.Clear();
DirectoryInfo _nodeDirectory = new DirectoryInfo(filePath);
ListViewItem.ListViewSubItem[] _subFolder;
ListViewItem _item = null;
try
{
foreach (DirectoryInfo i in _nodeDirectory.GetDirectories())
{
_item = new ListViewItem(i.Name, 1);
_subFolder = new ListViewItem.ListViewSubItem[] { new ListViewItem.ListViewSubItem(_item, "Directory") };
this.listViewFolder.Items.Add(_item);
}
foreach (FileInfo file in _nodeDirectory.GetFiles())
{
_item = new ListViewItem(file.Name, 2);
_subFolder = new ListViewItem.ListViewSubItem[] { new ListViewItem.ListViewSubItem(_item, "File") };
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show("Folder Access denied", ex.Message, MessageBoxButtons.OK);
}
}
}
答案 0 :(得分:0)
让自己头疼并将完整路径放在item.Tag属性中。然后你可以这样做:
string path = (string) ((ListViewItem)temp.SelectedItems[0].Tag;
您还可以检查路径是否是目录并采取相应措施。