How to browse for folder

前端 未结 4 1381
灰色年华
灰色年华 2020-12-18 04:30

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 wher

4条回答
  •  抹茶落季
    2020-12-18 04:49

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

提交回复
热议问题