Home  >  Article  >  Backend Development  >  How to obtain file path and folder path using browse button in C#

How to obtain file path and folder path using browse button in C#

黄舟
黄舟Original
2017-05-21 11:00:581602browse

This article mainly introduces the C# method of using the browse button to obtain the file path and folder path, and analyzes the C# browser event in combination with an example. For tips on response and file operations, friends who need them can refer to

This article describes how to use the browse button to obtain file paths and folder paths in C#. Share it with everyone for your reference, the details are as follows:

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

Generate file path

Create a new FolderDialog class (OverloadFolderNameEditor )

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("请选择一个文件夹");
    }
    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;
}

The above is the detailed content of How to obtain file path and folder path using browse button in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn