OpenFileDialog C# 自定义过滤器,如“ABC*.pdf”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19857824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenFileDialog C# custom filter like 'ABC*.pdf'
提问by user2071938
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
谢谢弗洛里安
采纳答案by Kjartan
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文件)。
回答by Sudhakar Tillapudi
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# 中创建自定义的打开文件对话框
回答by Noctis
Yes and no.
是和否。
No: Look at the MSDN, page. The filter is not used that way. It's only for the extensions.
否:查看MSDN页面。过滤器不是这样使用的。它仅用于扩展。
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,使用一些正则表达式来执行您想要的操作,然后简单地针对当前文件夹中的所有文件运行该匹配项(可能需要一些工作,但如果您真的想要它太糟糕了,去吧:))
回答by jAC
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,您必须使用DLLImport
Shell32.dll:
[DllImport("shell32.dll")]
The code provided in this SO questionshould 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/...
}
回答by rPulvi
Use this:
用这个:
Microsoft.Win32.OpenFileDialog myDialog. = new Microsoft.Win32.OpenFileDialog();
myDialog..DefaultExt = ".pdf";
myDialog.Filter = "FilesIWant (ABC*.pdf)|ABC*.pdf