How to view the folder

I want to create a program containing a browse button, where we can go to the selected folder and open the file inside the folder.

I need a link and reading, where can I solve my problems? How and what methods / class should I use. I do not prefer reading from MSDN to be difficult for me to understand their theory. FYI I'm still new to C #.

Many thanks

P / s: Here is the code I found on the Internet where you can browse / create a new folder. But I do not 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;
    }
}
+5
source share
4 answers

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);
        }
    }
}
+9
source
            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; 
            }
+3
source

From the toolbar, drag the FolderBrowserDialog component into your form and name it folderBrowserDialog. In the browse button's event handler, click the following code.

    private void btnBrowseBackupLocation_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            txtboxBackupLocation.Text = folderBrowserDialog.SelectedPath;
        }
    }
+1
source

To insert the file path by clicking the button with the name "Browse_Button" with the file name in the text field with the name "ARfilePath", the above code will be changed 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);
            }
        }
    }
0
source

All Articles