C#选取文件夹的对话框

首先要说明一下:

添加引用:   System.Design 

此文件在下面的位置 

C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Design.dll

有些人说在添加引用的面板中并找不到这个引用.这是由于你的目标框架设置错误

在 解决方案管理器上  右键 "属性" --> 应用程序 --> 目标框架 修改为 .Net Framework 4.0  (没有那个client  )

这样你再在添加引用面板中就可以找到这个引用了.

 

添加这两个引用

using System.Windows.Forms;
using System.Windows.Forms.Design;


 

 

 

==================新建一个FolderDialog类 ==============
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
   public class FolderDialog : FolderNameEditor
    {
        FolderNameEditor.FolderBrowser fDialog = new FolderNameEditor.FolderBrowser();
        public FolderDialog()
        {
        }
        public DialogResult DisplayDialog()
        {
            return DisplayDialog("请选择一个文件夹");
        }
        public DialogResult DisplayDialog(string description)
        {
            fDialog.Description = description;
            return fDialog.ShowDialog();
        }
        public string Path
        {
            get { return fDialog.DirectoryPath; }
        }
        ~FolderDialog() { fDialog.Dispose(); }
    }
--------------调用--------------------
            FolderDialog openFolder = new FolderDialog();
            if (openFolder.DisplayDialog() == DialogResult.OK)
            {
                textBox1.Text = openFolder.Path.ToString();
            }
            else
            {
                textBox1.Text = "你没有选择目录";
            }
            DirectoryInfo dirInfo = new DirectoryInfo(textBox1.Text);
            FileInfo[] files = dirInfo.GetFiles();
            foreach (FileInfo filename in files)
            {
                listBox1.Items.Add(filename);
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值