Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

c# - How to browse for folder

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...