首页 新闻 会员 周边 捐助

怎么从treeview中得到各文件的物理路径??

0
悬赏园豆:20 [已关闭问题]
<P>我在treeview中用递归方法生成了一个目录树,但我想从中得到选择的目录的物理路径!<BR>代码如下:请各位大侠指点迷津<BR>&nbsp;protected void Page_Load(object sender, EventArgs e)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!IsPostBack)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowFolderSchema();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P> <P>&nbsp;&nbsp;&nbsp; private void ShowFolderSchema()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string LocalFolderPath = Server.MapPath("");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (LocalFolderPath != string.Empty)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.treeView1.Nodes.Clear();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreeNode[] tn = LoadDirectoryInfo(LocalFolderPath);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach( TreeNode tvn in tn)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tvn.Value = Server.MapPath(tvn.Text);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.treeView1.Nodes.Add(tvn);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P> <P>&nbsp;&nbsp;&nbsp; internal TreeNode[] LoadDirectoryInfo(string folderPath)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DirectoryInfo di = new DirectoryInfo(folderPath);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DirectoryInfo[] dis = di.GetDirectories();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreeNode[] tn = new TreeNode[dis.Length];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; dis.Length; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tn[i] = new TreeNode(dis[i].Name);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tn[i].Value = Server.MapPath(dis[i].ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TreeNode[] TempTn = LoadDirectoryInfo(dis[i].FullName);</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach(TreeNode tvn in TempTn)<BR>&am
傅展鹏的主页 傅展鹏 | 初学一级 | 园豆:180
提问于:2007-12-26 22:33
< >
分享
其他回答(2)
0
不是有这一句吗:tn[i].Value = Server.MapPath(dis[i].ToString()); 你可以用treeView1.SelectedValue或者treeView1.SelectedNode.Value来获取选择的节点对应的路径。
deerchao | 园豆:8367 (大侠五级) | 2007-12-27 02:55
0
using System; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; TreeNode root = new TreeNode(); DirectoryInfo rootDir = new DirectoryInfo(Server.MapPath("~/")); AppendChild(rootDir, root); TreeView1.Nodes.Add(root); } protected void AppendChild(DirectoryInfo dir, TreeNode node) { DirectoryInfo[] dirs = dir.GetDirectories(); if (dirs.Length > 0) { for (int i = 0; i < dirs.Length; i++) { TreeNode sub = new TreeNode(dirs[i].Name); AppendChild(dirs[i], sub); node.ChildNodes.Add(sub); } } } protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { Response.Write( "当前的物理路径是 : " + Server.MapPath("~/" + TreeView1.SelectedNode.Text)); } }
沙加 | 园豆:3680 (老鸟四级) | 2007-12-27 10:28
0
http://www.codeproject.com/KB/tree/TreeViewWalker.aspx 去看看这篇文章吧,从遍历文件夹到定位节点都用了,取全路径更是信手拈来啦 这句就可以了:this.label1.Text = this.treeAdvanced.SelectedNode.FullPath;【WebForm是SelectedNode.ValuePath】
Justin | 园豆:980 (小虾三级) | 2007-12-29 17:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册