How to browse for folder

前端 未结 4 1378
灰色年华
灰色年华 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:48

    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);
                }
            }
        }
    
    0 讨论(0)
  • 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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 04:57

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-18 05:10
                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; 
                }
    
    0 讨论(0)
提交回复
热议问题