0

我是 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),文件永远不会打开。单击节点时没有任何反应。请帮忙。

4

1 回答 1

0

这些不是本地文件的有效 url。

参考:文件 URI 方案

对于网络位置:

file://hostname/path/to/the%20file.txt

或者对于本地文件,省略主机名,但不省略斜杠(注意第三个斜杠):

file:///c:/path/to/the%20file.txt

希望能为您解决。

于 2013-11-11T13:22:03.200 回答