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

黄舟
Release: 2017-05-14 10:31:44
Original
2525 people have browsed it

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);
      }
    }
  }
Copy after login

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();
    }
  }
}
Copy after login

Events under the browse button

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template