C#中,我想在结点上点击鼠标右键获得该结点的路径,应该怎么写代码?谢谢!
呵呵,谢谢,可是我还是不太懂,我没有用到数据库.就只是做了一个本机文件的treeView,现在就是想在结点上右击,弹出上下文菜单,获得被点击的结点的路径,以便对其进行一些别的操作.
恩,好方法,可是我对文件树的加载是这样进行的,以下是我的代码,怎么才能实现我的功能呢.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace Compress
{
public partial class FormFile : Form
{
private string nodeName;
public FormFile()
{
InitializeComponent();
}
private DirectoryInfo folder;
private void FormFile_Load(object sender, EventArgs e)
{
LoadTree();
}
private void LoadTree()
{
DirectoryInfo directory;
//clear the tree
//treeExplore.Nodes.Clear();
//Loop through the drive letter and find the available drive
foreach (string drive in Environment.GetLogicalDrives())
{
try
{
// get the directory information for this path
directory = new DirectoryInfo(drive);
//if the retrived directory is valid ,add it to the tree view
if (directory.Exists == true)
{
TreeNode newNode = new TreeNode(directory.FullName);
treeExplore.Nodes.Add(newNode);
//add the new node to the root level
GetSubDirectories(newNode);
}
}
catch
{
return;
}
}
}
private void GetSubDirectories(TreeNode parent)
{
DirectoryInfo directory;
try
{
if (parent.Nodes.Count == 0)
{
directory = new DirectoryInfo(parent.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode newNode = new TreeNode(dir.Name);
parent.Nodes.Add(newNode);
}
}
foreach (TreeNode Node in parent.Nodes)
{
if (Node.Nodes.Count == 0)
{
directory = new DirectoryInfo(Node.FullPath);
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode newnode = new TreeNode(dir.Name);
Node.Nodes.Add(newnode);
}
}
}
}
catch
{
return;
}
}
private void treeExplore_AfterSelect(object sender, TreeViewEventArgs e)
{
DirectoryInfo dirInfo = new DirectoryInfo(e.Node.FullPath);
listExplorer.Items.Clear();
if (dirInfo.Exists)
{
FileInfo[] fileInfo = dirInfo.GetFiles();
foreach (FileInfo info in fileInfo)
{
ListViewItem item = new ListViewItem();
item = listExplorer.Items.Add(info.Name);
item.SubItems.Add(info.LastAccessTime.ToString());
item.SubItems.Add(info.Length.ToString());
}
}
private void onMouseDown(object sender, MouseEventArgs e)
{
TreeNode newSelectedNode = treeExplore.GetNodeAt(e.X, e.Y);
if (newSelectedNode != null)
{
treeExplore.SelectedNode = newSelectedNode;
treeExplore.SelectedNode.ContextMenuStrip = contextMenuStrip; //哈哈,终于找到你了!
}
if (e.Button == MouseButtons.Right)
{
treeExplore.SelectedNode = treeExplore.GetNodeAt(e.X, e.Y);
contextMenuStrip.Visible = true;
contextMenuStrip
MessageBox.Show("abc");
}
}
private void compressMenuItem_Click(object sender,
TreeNodeMouseClickEventArgs e) //compressMenuItem是contextmenu的列表
{
string[] FileProperties = new string[2];
FileProperties[0] = "???"; //待解压的文件 这是最关键的,怎么才是正确获的文件的路径
FileProperties[1] = "???";//解压后放置的目标目录
ZipClass Zc = new ZipClass(); //调用一个压缩函数,实现对文件的压缩,
Zc.ZipFileMain(FileProperties); //函数的参数是文件的路径,现在的问题是怎么取的文件的路径
}
}
}
[此贴子已经被作者于2007-4-16 11:22:26编辑过]