6

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;
    }
}
Jannik
  • 2,310
  • 6
  • 32
  • 61
user147685
  • 471
  • 7
  • 16
  • 28
  • 6
    For the record: Not wanting to read MSDN docs when developing for Windows or .NET is the worst you can do. MSDN covers absolutely everything Windows and .NET. More important, it also tells you what you should not do, which may be especially important for you since you are a beginner. If you are not sure where in MSDN to look, use Google (or Bing) to search on MSDN. I'm sure there's a sample for this in MSDN that Google would have found for you. – OregonGhost Sep 14 '09 at 09:23
  • I realize that, but the problem is, i'm on self learning, there are explainations which i couldnt understand. So that's why i need to depend on the other resources. Thanks for advice! – user147685 Sep 15 '09 at 01:29
  • 1
    Keep reading until you understand. MSDN is your best friend. – banging Jul 15 '13 at 14:55
  • MSDN sucks. google => stackoverflow ftw – Ready Cent Feb 24 '16 at 16:52

4 Answers4

10

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);
        }
    }
}
Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
4
            string folderpath = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            fbd.ShowNewFolderButton = false;
            fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
            DialogResult dr = fbd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                folderpath = fbd.SelectedPath;
            }

            if (folderpath != "")
            {
                txtBoxPath.Text = folderpath; 
            }
Rae Lee
  • 1,321
  • 10
  • 11
2

From toolbox drag a FolderBrowserDialog component to your form and name it folderBrowserDialog. In your browse button event handler write following code.

    private void btnBrowseBackupLocation_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            txtboxBackupLocation.Text = folderBrowserDialog.SelectedPath;
        }
    }
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
0

To insert the file path on click of button named "Browse_Button" with file name in text box named "ARfilePath" the above code will be modified as:

    private void Browse_Button_Click(object sender, 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;
        Boolean FileExist=openFileDialog1.CheckFileExists;
        Boolean PathExist=openFileDialog1.CheckPathExists;
        openFileDialog1.FileName = null;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        if (FileExist == true && PathExist == true)
                        {
                            // Insert code to read the stream here.
                            string Pathname = openFileDialog1.FileName;
                            ARfilePath.Text = Pathname;
                            ARfilePath.Enabled = false;
                            /*DISABLED SO THAT THE USER CANNOT MAKE UNNECESSARY CHANGES IN THE FIELD*/
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                /*SHOW ERRORS TO USER*/ error_label.Text = "Error: Could not read file from disk. Original error: " + ex.Message;

                //MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219