如何浏览文件夹 [英] How to browse for folder

查看:133
本文介绍了如何浏览文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个程序包含浏览按钮,在这里我们可以浏览到所选择的文件夹并打开文件夹内的文件。



我需要一个参考和阅读在那里我能解决我的问题呢?像/班我应该用什么样的方法。我不喜欢从MSDN堂妹很难阅读我了解他们的理论。 。FYI我还在初学C#



非常感谢你。



P / S:这里是代码我从互联网上,其中u可以浏览/创建新的文件夹中。但我不知道为什么它使用的Shell32.dll ..

 私人无效的button1_Click(对象发件人,EventArgs五)
{
串strPath的;
串strCaption =选择一个目录和文件夹。
的DialogResult dlgResult;
Shell32.ShellClass SHL =新Shell32.ShellClass();
Shell32.Folder2 FLD =(Shell32.Folder2)shl.BrowseForFolder(0,strCaption,0,
System.Reflection.Missing.Value);
如果(FLD == NULL)
{
dlgResult = DialogResult.Cancel;
}
,否则
{
strPath的= fld.Self.Path;
dlgResult = DialogResult.OK;
}
}


解决方案

MSDN

 私人无效的button1_Click(对象发件人,发送System.EventArgs)
{
流myStream = NULL;
打开文件对话框openFileDialog1 =新的OpenFileDialog();

openFileDialog1.InitialDirectory =C:\\;
openFileDialog1.Filter =TXT文件(* .txt)| * .TXT |所有文件(*。*)| *。*;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = TRUE;

如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{

{
如果((myStream = openFileDialog1.OpenFile() )!= NULL)
{$ b使用(myStream)
{
//插入代码读到这里流$ b。
}
}
}
赶上(异常前)
{
MessageBox.Show(错误:无法从磁盘中读取文件原来的错误: + ex.Message);
}
}
}


I want to design a program contain browse button, where we can browse to the selected folder and open the file inside the folder.

I need a reference and reading where i can solve my problems? Like what methods/class should I use. I'm not prefer reading from MSDN coz hard for me to understand their theories. FYI i'm still beginner in C#.

Thank you very much

P/s: Here are code that I found from internet where u can browse/create new folder. But I dont know why it uses Shell32.dll..

private void button1_Click(object sender, EventArgs e)
{
    string strPath;
    string strCaption = "Select a Directory and folder.";
    DialogResult dlgResult;
    Shell32.ShellClass shl = new Shell32.ShellClass();
    Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0,
        System.Reflection.Missing.Value);
    if (fld == null)
    {
        dlgResult = DialogResult.Cancel;
    }
    else
    {
        strPath = fld.Self.Path;
        dlgResult = DialogResult.OK;
    }
}

解决方案

from msdn

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}

这篇关于如何浏览文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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