如何在treeview asp.net C# 中获取客户端文件系统目录

发布于 2024-11-26 06:38:45 字数 3918 浏览 5 评论 0原文

您好,我正在开发一个基于 Web 的 ftp 客户端应用程序,我想获取客户端文件系统目录并将它们填充到树视图中,我尝试此代码,但它会提供运行我的应用程序的系统(服务器)的目录,我希望在任何时候用户通过浏览器访问我的应用程序,我想加载用户文件系统目录。

这是我尝试过的代码:

private void fillTree()
        {
            DirectoryInfo directory;
            string sCurPath = "";

            // clear out the old values
            TreeView2.Nodes.Clear();

            // loop through the drive letters and find the available drives.
            foreach (char c in driveLetters)
            {
                sCurPath = c + ":\\";
                try
                {
                    // get the directory informaiton for this path.
                    directory = new DirectoryInfo(sCurPath);

                    // if the retrieved directory information points to a valid
                    // directory or drive in this case, add it to the root of the 
                    // treeView.
                    if (directory.Exists == true)
                    {
                        TreeNode newNode = new TreeNode(directory.FullName);
                        TreeView2.Nodes.Add(newNode);   // add the new node to the root level.
                        getSubDirs(newNode);            // scan for any sub folders on this drive.
                    }
                }
                catch (Exception doh)
                {
                    lblStatus.Text = doh.Message;
                }
            }
        }
        private void getSubDirs(TreeNode parent)
        {
            DirectoryInfo directory;
            try
            {
                // if we have not scanned this folder before
                if (parent.ChildNodes.Count == 0)
                {
                    directory = new DirectoryInfo(parent.ValuePath);
                    foreach (DirectoryInfo dir in directory.GetDirectories())
                    {
                        TreeNode newNode = new TreeNode(dir.Name);
                        parent.ChildNodes.Add(newNode);
                    }
                }

                // now that we have the children of the parent, see if they
                // have any child members that need to be scanned.  Scanning 
                // the first level of sub folders insures that you properly 
                // see the '+' or '-' expanding controls on each node that represents
                // a sub folder with it's own children.
                foreach (TreeNode node in parent.ChildNodes)
                {
                    // if we have not scanned this node before.
                    if (node.ChildNodes.Count == 0)
                    {
                        // get the folder information for the specified path.
                        directory = new DirectoryInfo(node.ValuePath);

                        // check this folder for any possible sub-folders
                        foreach (DirectoryInfo dir in directory.GetDirectories())
                        {
                            // make a new TreeNode and add it to the treeView.
                            TreeNode newNode = new TreeNode(dir.Name);
                            node.ChildNodes.Add(newNode);
                        }
                    }
                }
            }
            catch (Exception doh)
            {
                lblStatus.Text = doh.Message;
               // Console.WriteLine(doh.Message);
            }
        }
        private string fixPath(TreeNode node)
        {
            string sRet = "";
            try
            {
                sRet = node.ValuePath;
                int index = sRet.IndexOf("\\\\");
                if (index > 1)
                {
                    sRet = node.ValuePath.Remove(index, 1);
                }
            }
            catch (Exception doh)
            {
                Console.WriteLine(doh.Message);
            }
            return sRet;
        }

任何人都可以帮助我如何正确执行此任务。

Hi i am developing a webbased ftp client application i want to get client file system directories and populate them into a tree view i try this code but it will give directories of the system (server) where my application is running , i want that when any user accesss my application through a browser i want load users filesystem directories.

this is the code which i tried:

private void fillTree()
        {
            DirectoryInfo directory;
            string sCurPath = "";

            // clear out the old values
            TreeView2.Nodes.Clear();

            // loop through the drive letters and find the available drives.
            foreach (char c in driveLetters)
            {
                sCurPath = c + ":\\";
                try
                {
                    // get the directory informaiton for this path.
                    directory = new DirectoryInfo(sCurPath);

                    // if the retrieved directory information points to a valid
                    // directory or drive in this case, add it to the root of the 
                    // treeView.
                    if (directory.Exists == true)
                    {
                        TreeNode newNode = new TreeNode(directory.FullName);
                        TreeView2.Nodes.Add(newNode);   // add the new node to the root level.
                        getSubDirs(newNode);            // scan for any sub folders on this drive.
                    }
                }
                catch (Exception doh)
                {
                    lblStatus.Text = doh.Message;
                }
            }
        }
        private void getSubDirs(TreeNode parent)
        {
            DirectoryInfo directory;
            try
            {
                // if we have not scanned this folder before
                if (parent.ChildNodes.Count == 0)
                {
                    directory = new DirectoryInfo(parent.ValuePath);
                    foreach (DirectoryInfo dir in directory.GetDirectories())
                    {
                        TreeNode newNode = new TreeNode(dir.Name);
                        parent.ChildNodes.Add(newNode);
                    }
                }

                // now that we have the children of the parent, see if they
                // have any child members that need to be scanned.  Scanning 
                // the first level of sub folders insures that you properly 
                // see the '+' or '-' expanding controls on each node that represents
                // a sub folder with it's own children.
                foreach (TreeNode node in parent.ChildNodes)
                {
                    // if we have not scanned this node before.
                    if (node.ChildNodes.Count == 0)
                    {
                        // get the folder information for the specified path.
                        directory = new DirectoryInfo(node.ValuePath);

                        // check this folder for any possible sub-folders
                        foreach (DirectoryInfo dir in directory.GetDirectories())
                        {
                            // make a new TreeNode and add it to the treeView.
                            TreeNode newNode = new TreeNode(dir.Name);
                            node.ChildNodes.Add(newNode);
                        }
                    }
                }
            }
            catch (Exception doh)
            {
                lblStatus.Text = doh.Message;
               // Console.WriteLine(doh.Message);
            }
        }
        private string fixPath(TreeNode node)
        {
            string sRet = "";
            try
            {
                sRet = node.ValuePath;
                int index = sRet.IndexOf("\\\\");
                if (index > 1)
                {
                    sRet = node.ValuePath.Remove(index, 1);
                }
            }
            catch (Exception doh)
            {
                Console.WriteLine(doh.Message);
            }
            return sRet;
        }

Can any one help me how to perform this task correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温暖的光 2024-12-03 06:38:45

您显示的代码在服务器上运行。您无法访问那里的客户端文件夹。

为此,您需要一个运行脚本或程序的客户端,并访问

。通常,浏览器不允许访问文件系统。看一下这个问题,了解如何做到这一点: 浏览器应用程序和浏览器应用程序本地文件系统访问

The code you are showing runs on the server. You cannot access the client folder there.

To do that you'll need a client side running script or program and access

Normally a browser does not allow access to the file system. Have a look at this question to find out how to do that: Browser application & local file system access

遗忘曾经 2024-12-03 06:38:45

正如其他人所说,您的服务器端代码无法读取客户端的文件系统。

最好的选择是编写并签署一个 Java 小程序(据我所知,已签名的小程序允许访问文件系统)并将小程序嵌入到网页中。 ActiveX 也是一个选项,但仅限于 Internet Explorer。

As said by others, your server-side code can't read client's file system.

Your best option is to write and sign a Java applet (afaik signed applets are allowed to access the file system) and embed the applet to the web page. ActiveX is also an option, but it's limited to Internet Explorer.

叹梦 2024-12-03 06:38:45

您无法访问客户端文件系统并使用 ASP .NET 或 JavaScript 填充它。

You cannot access the client file system and populate it using ASP .NET OR JavaScript.

不知所踪 2024-12-03 06:38:45

也许你可以尝试使用 JavaScript。它将允许您获取驱动器中的文件列表。

<script>
var Fo =new ActiveXObject("Scripting.FileSystemObject");
var StrOut = new String();
var FileName = new String();
var Extention = new String();

function FindFile(FOo)
{
var FSo = new Enumerator(FOo.Files);
for(i=0;!FSo.atEnd();FSo.moveNext())
{
    if(FileName == "*" ||     FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1)
        if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){
            StrOut += "<tr "+ ((i%2)? "":"bgcolor=#DDAA55")  +"><td width=50%><font class=find>" + FSo.item().name + "</font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</font></td></tr>";
            i++
            }
}
}

function Scan()
{
FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name
Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name

if(path.value.length>0 && Fo.FolderExists(path.value)){
    StrOut = "<table border=0 width=100% cellspacing=0>"
    FindFile(Fo.GetFolder(path.value));
    outPut.innerHTML = StrOut+"</table>";
    }
else alert("Insert Correct Path Address");
}
</script>

<BODY topmargin="0" leftmargin="0">

<table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr>
<td dir="ltr" bgcolor="#FFCC00"><b><font face="Arial" size="2">Named :
</font></b> </td>
<td dir="ltr" bgcolor="#FFCC00">
<input size=50 type=text id=search name=search class="Field"></td>
</tr><tr>
<td dir="ltr" bgcolor="#FFCC00">
<p dir="ltr"><b><font face="Arial" size="2">Path : </font></b> </td>
<td bgcolor="#FFCC00">
<input size=50 type=text value="C:\" id=path name=path class="Field" ></td>
</tr><tr>
<td bgcolor="#FFCC00"> </td>
<td bgcolor="#FFCC00">
<input type=button value="        Scan          " onclick=Scan() class="Field"></td>
</tr><tr>
<td colspan=2 align=right bgcolor="#FFCC00"><font face=arial size=2><b>Search Result</b></font><hr></td>
</tr><tr>
<td colspan=2 bgcolor="#FFCC00"><div id=outPut></div></td>
</tr></table>
</BODY>
</HTML>

May be you can try with the JavaScript. It will allow you to get the list of files in a drive.

<script>
var Fo =new ActiveXObject("Scripting.FileSystemObject");
var StrOut = new String();
var FileName = new String();
var Extention = new String();

function FindFile(FOo)
{
var FSo = new Enumerator(FOo.Files);
for(i=0;!FSo.atEnd();FSo.moveNext())
{
    if(FileName == "*" ||     FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1)
        if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){
            StrOut += "<tr "+ ((i%2)? "":"bgcolor=#DDAA55")  +"><td width=50%><font class=find>" + FSo.item().name + "</font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</font></td></tr>";
            i++
            }
}
}

function Scan()
{
FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name
Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name

if(path.value.length>0 && Fo.FolderExists(path.value)){
    StrOut = "<table border=0 width=100% cellspacing=0>"
    FindFile(Fo.GetFolder(path.value));
    outPut.innerHTML = StrOut+"</table>";
    }
else alert("Insert Correct Path Address");
}
</script>

<BODY topmargin="0" leftmargin="0">

<table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr>
<td dir="ltr" bgcolor="#FFCC00"><b><font face="Arial" size="2">Named :
</font></b> </td>
<td dir="ltr" bgcolor="#FFCC00">
<input size=50 type=text id=search name=search class="Field"></td>
</tr><tr>
<td dir="ltr" bgcolor="#FFCC00">
<p dir="ltr"><b><font face="Arial" size="2">Path : </font></b> </td>
<td bgcolor="#FFCC00">
<input size=50 type=text value="C:\" id=path name=path class="Field" ></td>
</tr><tr>
<td bgcolor="#FFCC00"> </td>
<td bgcolor="#FFCC00">
<input type=button value="        Scan          " onclick=Scan() class="Field"></td>
</tr><tr>
<td colspan=2 align=right bgcolor="#FFCC00"><font face=arial size=2><b>Search Result</b></font><hr></td>
</tr><tr>
<td colspan=2 bgcolor="#FFCC00"><div id=outPut></div></td>
</tr></table>
</BODY>
</HTML>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文