WPF对话框,用于选择文件或文件夹

WPF对话框,用于选择文件或文件夹,wpf,dialog,Wpf,Dialog,我想知道是否有办法让用户在点击“选择文件或文件夹”按钮时,根据自己的意愿选择文件或文件夹。 我知道有一种方法可以创建一个选择文件或选择文件夹,我想用一种方法,用户可以选择一个文件,或者选择一个文件夹,然后在我的代码中,我可以得到该文件或文件夹的文件列表 提前感谢您的帮助这有点糟糕,但我认为您需要创建自己的OpenFileDialogue并使用Directory类 using System.IO; string[] filePaths = Directory.GetFiles(@"c

我想知道是否有办法让用户在点击“选择文件或文件夹”按钮时,根据自己的意愿选择文件或文件夹。 我知道有一种方法可以创建一个选择文件或选择文件夹,我想用一种方法,用户可以选择一个文件,或者选择一个文件夹,然后在我的代码中,我可以得到该文件或文件夹的文件列表


提前感谢您的帮助

这有点糟糕,但我认为您需要创建自己的OpenFileDialogue并使用Directory类

   using System.IO;

   string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
   string[] dirPaths = Directory.GetDirectories(@"c:\MyDir\");

directory类有一些有用的东西,可以使这个过程变得更简单。

它有点糟糕,但我认为您需要创建自己的OpenFileDialogue并使用directory类

   using System.IO;

   string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
   string[] dirPaths = Directory.GetDirectories(@"c:\MyDir\");

目录类有一些有用的东西,可以使这个过程变得更简单。

创建一个类
FolderPicker
然后像这样编码>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AutoRunGenerator
{
    class FolderPicker
    {
        public static string FileName = "";

        public FolderPicker()
        { 
          OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr= ofd.ShowDialog();
            string filename = ofd.FileName;
            FileName = filename;
        }
        public string GetFileName()
        {
            return FileName;
        }
    }
}
FolderPicker fp = new FolderPicker();
txtBox.Text= fp.GetFileName();
然后 从您的事件方法或类似于此的任何地方调用它>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AutoRunGenerator
{
    class FolderPicker
    {
        public static string FileName = "";

        public FolderPicker()
        { 
          OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr= ofd.ShowDialog();
            string filename = ofd.FileName;
            FileName = filename;
        }
        public string GetFileName()
        {
            return FileName;
        }
    }
}
FolderPicker fp = new FolderPicker();
txtBox.Text= fp.GetFileName();

我希望这对文件对话框有帮助

创建一个类
FolderPicker
然后像这样编码>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AutoRunGenerator
{
    class FolderPicker
    {
        public static string FileName = "";

        public FolderPicker()
        { 
          OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr= ofd.ShowDialog();
            string filename = ofd.FileName;
            FileName = filename;
        }
        public string GetFileName()
        {
            return FileName;
        }
    }
}
FolderPicker fp = new FolderPicker();
txtBox.Text= fp.GetFileName();
然后 从您的事件方法或类似于此的任何地方调用它>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AutoRunGenerator
{
    class FolderPicker
    {
        public static string FileName = "";

        public FolderPicker()
        { 
          OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr= ofd.ShowDialog();
            string filename = ofd.FileName;
            FileName = filename;
        }
        public string GetFileName()
        {
            return FileName;
        }
    }
}
FolderPicker fp = new FolderPicker();
txtBox.Text= fp.GetFileName();

我希望这将有助于文件对话框

好的,您能再给我解释一下您的解决方案吗。我想打开一个对话框,用户可以在其中选择一个文件夹并单击“确定”,或者选择文件夹中的一些或一个文件并选择“确定”。在第一种情况下,您将获得文件夹中所有文件的字符串路径列表,在第二种情况下,您将获得仅选定文件的字符串路径列表。确定使用列表框创建新窗口或其他内容。将这两个列表添加到列表框中,当用户选择某个内容时,请检查它是否是文件或文件夹(我相信可以通过目录类完成),并根据您的需要进行处理。好的,请您进一步解释您的解决方案。我想打开一个对话框,用户可以在其中选择一个文件夹并单击“确定”,或者选择文件夹中的一些或一个文件并选择“确定”。在第一种情况下,您将获得文件夹中所有文件的字符串路径列表,在第二种情况下,您将获得仅选定文件的字符串路径列表。确定使用列表框创建新窗口或其他内容。将这两个列表添加到列表框中,当用户选择某个内容时,检查该内容是否为文件或文件夹(我相信可以通过目录类完成),并根据您的需要进行处理。您将其命名为“FolderPicker”的事实并不能使其神奇地允许选择文件夹。OP要求使用多用途对话框,您只需将文件对话框包装为自己的类型。您将其命名为“FolderPicker”并不能使其神奇地允许选择文件夹。OP要求使用多用途对话框,您只需将文件对话框包装为自己的类型。