简体   繁体   English

OpenFileDialog C# 自定义过滤器,如 'ABC*.pdf'

[英]OpenFileDialog C# custom filter like 'ABC*.pdf'

Is it possible to specify custom filters like 'ABC*.pdf' which means: " Show all PDF which starts with ABC "?是否可以指定自定义过滤器,例如'ABC*.pdf' ,这意味着:“显示所有以 ABC 开头的 PDF ”?

I can only specify *.pdf , *.doc , *.* , etc.我只能指定*.pdf*.doc*.*等。

Thanks Florian谢谢弗洛里安

Updated更新

Changed my solution a little after realizing the following would be better:在意识到以下内容会更好后,稍微改变了我的解决方案:

This is not a complete "hard filter", but making use of the FileName property should still cover your needs:这不是一个完整的“硬过滤器”,但使用FileName属性仍应满足您的需求:

using System;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "pro*";
            this.openFileDialog1.Filter = "Pdf Files|*.pdf";
            this.openFileDialog1.ShowDialog();
        }
    }
}

I suppose this might depend on which OS you are working with, but it did work in my case any way, on Windows 8.我想这可能取决于您使用的操作系统,但在我的情况下,它确实在 Windows 8 上工作。

I also realize that this does not filter out all irrelevant files "permanently", but it does at least provide an initial filter.我也意识到这不会“永久”过滤掉所有不相关的文件,但它至少提供了一个初始过滤器。

Result:结果:
( Without pro* in the FileName-field, this will show several other PDF files ). 如果文件名字段中没有pro* ,这将显示其他几个 PDF 文件)。

在此处输入图片说明

Yes and no.是和否。

No : Look at the MSDN , page.:查看MSDN页面。 The filter is not used that way.过滤器不是这样使用的。 It's only for the extensions.它仅用于扩展。

Yes : You could write your own class that extends/mimics the OpenFileDialog, have some regular expressions to do what you want, and simply run that match against all the files in the current folder (Might take some work, but if you really want it so bad, go for it :) ):您可以编写自己的类来扩展/模仿 OpenFileDialog,使用一些正则表达式来执行您想要的操作,然后简单地针对当前文件夹中的所有文件运行该匹配项(可能需要一些工作,但如果您真的想要它太糟糕了,去吧:))

As stated in my comment:正如我的评论中所述:

Unfortunately it's not possible.不幸的是,这是不可能的。 But you can create your own FileDialog但是您可以创建自己的 FileDialog

To create your own FileDialog, you can use the following methods:要创建自己的 FileDialog,可以使用以下方法:

  string[] Directories = Directory.GetDirectories(Path);
  string[] Files = Directory.GetFiles(Path);

Now filter the Files -Array to your specifications:现在根据您的规格过滤Files -Array:

List<string> wantedFiles = Files.ToList().Where(x => x.StartsWith("ABC"));

To get the file Icons, you have to use the DLLImport of Shell32.dll:要获取文件 Icons,您必须使用 Shell32.dll 的DLLImport

[DllImport("shell32.dll")]

The code provided in this SO question should solve the problem.SO 问题中提供的代码应该可以解决问题。

A project that implements own FileDialogs written by my brother can be found here: Download project可以在此处找到实现我兄弟编写的自己的 FileDialogs 的项目下载项目

In short, this should do the trick:简而言之,这应该可以解决问题:

foreach (string file in Directory.GetFiles(Path)
                                 .Where(x => new DirectoryInfo(x).Name.StartsWith("ABC")))
{
    //Add the string to your ListView/ListBox/...
}

Answer is straight forward : NO答案很简单:

You can set the Filters to allow only specific File Types with property Filter asbelow :您可以将过滤器设置为仅允许具有Filter属性的特定文件类型,如下所示:

fileOpenDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

but filtering file names is NOT Possible.但过滤的文件名是NOT可能。

You can create your own Custom OpenFIleDialog in that case.在这种情况下,您可以创建自己的自定义OpenFIleDialog

See this Link for more info : How to create customized open file dialog in C#有关更多信息,请参阅此链接: 如何在 C# 中创建自定义的打开文件对话框

I checked it again and it seems for .NET 5.0 it works just as OP expected it.我再次检查了它,似乎对于 .NET 5.0 它的工作方式与 OP 预期的一样。

var fileSelector = new OpenFileDialog()
{
  Filter = "ABC Files|abc*.pdf";  
};

With this the FileDialog will only show .pdf files beginning with "abc".这样,FileDialog 将只显示以“abc”开头的 .pdf 文件。

Use this:使用这个:

Microsoft.Win32.OpenFileDialog myDialog. = new Microsoft.Win32.OpenFileDialog();
myDialog..DefaultExt = ".pdf";
myDialog.Filter = "FilesIWant (ABC*.pdf)|ABC*.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM