c# - 如何从 OpenFileDialog 和 FolderBrowserDialog 获取文件路径?

标签 c# openfiledialog folderbrowserdialog

嘿,几天前我开始学习 C#,我正在尝试制作一个程序,将文件复制和粘贴(如果需要,还可以替换)到选定的目录,但我不知道如何获取目录和文件来自 openfiledialog 和 folderbrowserdialog 的路径

我做错了什么?

代码如下:

namespace filereplacer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void direc_Click(object sender, EventArgs e)
        {
            string folderPath = "";
            FolderBrowserDialog directchoosedlg = new FolderBrowserDialog();
            if (directchoosedlg.ShowDialog() == DialogResult.OK)
            {
                folderPath = directchoosedlg.SelectedPath;
            }
        }

        private void choof_Click(object sender, EventArgs e)
        {

            OpenFileDialog choofdlog = new OpenFileDialog();
            choofdlog.Filter = "All Files (*.*)|*.*";
            choofdlog.FilterIndex = 1;

            choofdlog.Multiselect = true;
            choofdlog.ShowDialog();
        }

        private void replacebtn_Click(object sender, EventArgs e)
        {
          // This is where i'm having trouble
        }

        public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
        {
            File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);
        }
    }

最佳答案

对于OpenFileDialog :

OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.*)|*.*";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;

if (choofdlog.ShowDialog() == DialogResult.OK)    
{     
    string sFileName = choofdlog.FileName; 
    string[] arrAllFiles = choofdlog.FileNames; //used when Multiselect = true           
}

对于FolderBrowserDialog :

FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Custom Description"; 

if (fbd.ShowDialog() == DialogResult.OK)
{
    string sSelectedPath = fbd.SelectedPath;
}

要访问选定的文件夹选定的文件名,您可以在类级别声明这两个字符串。

namespace filereplacer
{
   public partial class Form1 : Form
   {
      string sSelectedFile;
      string sSelectedFolder;

      public Form1()
      {
         InitializeComponent();
      }

      private void direc_Click(object sender, EventArgs e)
      {
         FolderBrowserDialog fbd = new FolderBrowserDialog();
         //fbd.Description = "Custom Description"; //not mandatory

         if (fbd.ShowDialog() == DialogResult.OK)      
               sSelectedFolder = fbd.SelectedPath;
         else
               sSelectedFolder = string.Empty;    
      }

      private void choof_Click(object sender, EventArgs e)
      {
         OpenFileDialog choofdlog = new OpenFileDialog();
         choofdlog.Filter = "All Files (*.*)|*.*";
         choofdlog.FilterIndex = 1;
         choofdlog.Multiselect = true;

         if (choofdlog.ShowDialog() == DialogResult.OK)                 
             sSelectedFile = choofdlog.FileName;            
         else
             sSelectedFile = string.Empty;       
      }

      private void replacebtn_Click(object sender, EventArgs e)
      {
          if(sSelectedFolder != string.Empty && sSelectedFile != string.Empty)
          {
               //use selected folder path and file path
          }
      }
      ....
}

注意:

因为您保留了 choofdlog.Multiselect=true;,这意味着在 OpenFileDialog() 中您可以选择多个文件(通过按 ctrl 键和鼠标左键单击进行选择)。

在这种情况下,您可以在 string[] 中获取所有选定的文件:

在类里面:

string[] arrAllFiles;

定位这一行(当Multiselect=true这一行只给出第一个文件):

sSelectedFile = choofdlog.FileName; 

要获取所有文件,请使用:

arrAllFiles = choofdlog.FileNames; //this line gives array of all selected files

关于c# - 如何从 OpenFileDialog 和 FolderBrowserDialog 获取文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449988/

相关文章:

ms-access - 如何从 VBA 中的文件对话框对象中获取单个文件名(对于 MS Access 2007)?

c# - 如何选择已使用 OpenFileDialog 打开的文件

c# - OpenFileDialog 和 UnauthorizedAccessException

winforms - FolderBrowserDialog 置于最前面

c# - 如何将 JSON 对象数组反序列化为 C# 匿名类型?

c# - WPF DataGrid 绑定(bind) DataGridCell 内容

c# - UnsafeNativeMethods.IWebBrowser2.Navigate2 中的 HRESULT E_FAIL

c# - 发出 IL 代码以加载十进制值

delphi - 使用 Delphi 创建文件夹选择对话框的最常见方法是什么?

c# - 在文件路径中使用环境变量