如何使用c#.net和asp.net浏览目录 [英] How to browse directory using c#.net and asp.net

查看:78
本文介绍了如何使用c#.net和asp.net浏览目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我是Sidhanta。我有一个按钮和一个文本框。点击按钮时,它将打开一个目录对话框,路径将被复制到文本框。我得到了一些解决方案,但他们没有工作。

Hi Friends ,

I am Sidhanta. I have a button and a text box.While clicking on the button it will open a Directory Dialog and the path will be copied to text box. I got some solution but they are not working.

Dialog.FolderBrowser fBrowser = new Dialog.FolderBrowser();
           fBrowser.Description = "Select the Folder that you like.";
           fBrowser.StartLocation = OpenFileDialog.FolderBrowser.fbFolder.Desktop;
           fBrowser.Style = Dialogs.FolderBrowser.fbStyles.RestrictToSubfolders;
           //DialogResult result = fBrowser.ShowDialog();
           if (fBrowser.ShowDialog() == DialogResult.OK)
           {
               txtDirectory.Text = fBrowser.DirectoryPath;

           }





As Dialog 来自 System.Windows.Forms 。但它给出了错误,因为找不到类型或命名空间(你是否缺少using指令或汇编引用)

请指导我获取确切的解决方案。



有问候

Sidhanta Tripathy



As Dialog is coming under System.Windows.Forms. But it gives error as Type or namespace could not found(are you missing a using directive or an assembly reference).
Kindly guides me to get the exact solution.

With Regards
Sidhanta Tripathy

推荐答案

你好朋友...



请尝试下面的代码。

Hi friend...

Please try the below code .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Data;
using System.Configuration;

public partial class Fileinfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode onjparent = new TreeNode("D:\\", "D:\\");
            onjparent.PopulateOnDemand = true;
            TreeView1.Nodes.Add(onjparent );
            TreeView1.CollapseAll();

       }

        Errorlbl.Visible = false;
        TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
        TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);

    
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {

    }
    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {

        if (e.Node.Value.EndsWith("\\"))
        {

            Addnodes(e.Node.Value, e.Node);

        }
    }

    private TreeNode Addnodes(string path, TreeNode parentnode)
    {
       

        Filelist  objList = new FileList(path, "*.*");


        TreeNode node = new TreeNode(path, path);


        for (int index = 0; index < objList.Directories.Length; index++)
        {

            string directory = objList.Directories[index];

            TreeNode objchildnode = new TreeNode(directory, path + "\\" + directory + "\\");
            objchildnode.PopulateOnDemand = true;

            objchildnode.Target = "_blank";
            parentnode.ChildNodes.Add(objchildnode);
        }
        foreach (string file in objList.files)
        {
            TreeNode objchildnode = new TreeNode(file, path + "\\" + file);
            parentnode.ChildNodes.Add(objchildnode);
        }


        return node;

    }


    protected void btnbrows_Click(object sender, ImageClickEventArgs e)
    {
        TreeView1.Nodes.Clear();

        if (UpdateBrowseTextBoxWithSlash())
        {
            TreeNode onjparent = new TreeNode(txtbrow.Text, txtbrow.Text);
            onjparent.PopulateOnDemand = true;
            TreeView1.Nodes.Add(onjparent);
            TreeView1.CollapseAll();



        }

        else {


            Errorlbl.Visible = true;

            Errorlbl.Text = "please enter valid path";

        }

    }

    private bool UpdateBrowseTextBoxWithSlash()
    {
        if (txtbrow.Text.Length != 0)

        { 
			if(
                -1==txtbrow.Text.LastIndexOf (".") && !txtbrow.Text.Substring (txtbrow.Text.Length-1,1).Equals ("/") &&

                !txtbrow.Text.Substring (txtbrow.Text.Length-1,1).Equals ("\\")
                )
            {
                if (txtbrow.Text.Substring(0,1).Equals ("\\")|| -1!=txtbrow.Text.IndexOf(":\\"))
                txtbrow.Text +="\\";

            else

            txtbrow.Text +="/";
                
            return System.IO.Directory.Exists(txtbrow.Text);

            }
            else if(txtbrow.Text.LastIndexOf(".") >0)
            {
            
                return System.IO.File.Exists(txtbrow.Text);

            }
        
        }

        return true;
   
    
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        txtbrow.Text = TreeView1.SelectedValue;

    }
}


这篇关于如何使用c#.net和asp.net浏览目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆