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

use*_*981 22 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Has*_*san 43

对于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           
}
Run Code Online (Sandbox Code Playgroud)

对于FolderBrowserDialog:

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

if (fbd.ShowDialog() == DialogResult.OK)
{
    string sSelectedPath = fbd.SelectedPath;
}
Run Code Online (Sandbox Code Playgroud)

要访问selected folder,selected file name您可以在类级别声明两个字符串.

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
          }
      }
      ....
}
Run Code Online (Sandbox Code Playgroud)

注意:

正如您所保留的那样choofdlog.Multiselect=true;,这意味着OpenFileDialog()您可以选择多个文件(通过ctrl按键和鼠标左键单击进行选择).

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

在班级:

string[] arrAllFiles;
Run Code Online (Sandbox Code Playgroud)

找到此行(当Multiselect=true此行仅提供第一个文件时):

sSelectedFile = choofdlog.FileName; 
Run Code Online (Sandbox Code Playgroud)

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

arrAllFiles = choofdlog.FileNames; //this line gives array of all selected files
Run Code Online (Sandbox Code Playgroud)


Moe*_*bai 5

使用Path来自System.IO. 它包含操作文件路径的有用调用,包括GetDirectoryName执行您想要的操作,返回文件路径的目录部分。

用法很简单。

string directoryPath = System.IO.Path.GetDirectoryName(choofdlog.FileName);
Run Code Online (Sandbox Code Playgroud)