C Using the Browse button to get the file path and folder path

  • 2021-12-13 09:06:38
  • OfStack

This article illustrates how C # uses the Browse button to get file paths and folder paths. Share it for your reference, as follows:

Build folder path


private void btnChoose_Click(object sender, EventArgs e)
{
  using (OpenFileDialog dialog = new OpenFileDialog())
  {
    dialog.Multiselect = true;
    if (dialog.ShowDialog() == DialogResult.OK)
    {
      try
      {
        this.tbFilePath.Text = dialog.FileName;
      }
      catch(Exception ex)
      {
        throw(ex);
      }
    }
  }

Build file path

Create a new FolderDialog class (overloaded FolderNameEditor)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Windows.Forms;
namespace  Common
{
  class FolderDialog:FolderNameEditor
  {
    FolderBrowser fDialog = new FolderBrowser();
    public FolderDialog(){ }
    public DialogResult DisplayDialog()
    {
      return DisplayDialog(" Please select 1 Folders ");
    }
    public DialogResult DisplayDialog(string description)
    {
      fDialog.Description = description;
      return fDialog.ShowDialog();
    }
    public string Path
    {
      get
      {
        return fDialog.DirectoryPath;
      }
    }
    ~FolderDialog()
    {
      fDialog.Dispose();
    }
  }
}

Events under the Browse button


private void btnChoose_Click(object sender, EventArgs e)
{
  FolderDialog fDialog = new FolderDialog();
  fDialog.DisplayDialog();
  this.tbfilePath.Text = fDialog.Path;
}

More readers interested in C # can check the topic of this site: "C # File Operation Skills Summary", "C # Traversal Algorithm and Skills Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "C # XML File Operation Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: