我是 Treeview 的新手。我想在树视图中显示本地目录中的日志文件并单击它,文件应该打开。下面是我的代码:
1)我的aspx页面:
<asp:TreeView ID="tvLogResults" runat="server" ExpandDepth="1" OnSelectedNodeChanged="tvLogResults_SelectedNodeChanged" style="margin-left: 33px; margin-top: 16px;">
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="0px"
NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<RootNodeStyle ImageUrl="~/Styles/imgs/open-folder.jpg" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>
2) 代码隐藏
protected void btnSearch_Click(object sender, EventArgs e)
{
TreeNode n = new TreeNode();
n.Value = "0";
n.Text = "LogFiles-Datewise";
n.SelectAction = TreeNodeSelectAction.Select;
tvLogResults.Nodes.Add(n);
string data = txtFromDate.Text;
DateTime fromDate = DateTime.ParseExact(data, "MM/dd/yyyy", null);
DateTime toDate = DateTime.Parse(txtToDate.Text);
string path = string.Concat(HttpRuntime.AppDomainAppPath, @"LogFiles");
var files = from c in currDirectory.GetFiles()
where c.CreationTime >= fromDate && c.CreationTime <= toDate
select c;
int i = 1;
foreach (FileInfo file in files)
{
DateTime lastAccessTime = file.LastAccessTime;
TreeNode child = new TreeNode();
child.Value = file.FullName;
child.Text = lastAccessTime.ToString("MM/dd/yyyy");
child.PopulateOnDemand = true;
child.NavigateUrl = file.FullName;
child.SelectAction = TreeNodeSelectAction.Select;
tvLogResults.Nodes[0].ChildNodes.Add(child);
i++;
}
}
protected void tvLogResults_SelectedNodeChanged(object sender, EventArgs e)
{
string filename = Server.MapPath(tvLogResults.SelectedValue);
System.IO.FileInfo file = new System.IO.FileInfo(filename);
if (file.Exists)
{
System.Diagnostics.Process.Start(file.FullName);
}
}
即使在选择节点时,完整路径显示为“file:\\C:\TraceFiles\Log11Nov.txt”(IE &Chrome),文件永远不会打开。单击节点时没有任何反应。请帮忙。