C#:openfiledialog 过滤问题
当用户单击“打开文件”时,我正在编写代码,我想仅将文件类型过滤为 .txt,因此我执行了以下代码,但它不起作用,它根本不显示任何 txt 文件,只是一个空文件夹,这是代码:
try
{
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Text File | .txt";
o.InitialDirectory = Application.StartupPath;
o.ShowDialog();
}
catch
{}
i am writing a code when a user click open file , i would like to filter the type of files to .txt only so i did the following code , but it didnt work it didnt show any txt files at all , just an empty folder , here is the code :
try
{
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Text File | .txt";
o.InitialDirectory = Application.StartupPath;
o.ShowDialog();
}
catch
{}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只会显示名为
.txt
的文件,即一个空格(它可能会忽略?),后跟一个点和“txt”。删除
|
周围的空格,并添加通配符,以便匹配多个文件。This will only show files named
.txt
, i.e., a single space (which it might ignore?) followed by a dot and "txt".Remove the spaces around the
|
, and add a wildcard so you match more than one file.您需要为过滤器使用通配符,如下所示:
You need a wildcard for the filter, like this: