OpenFileDialog - 仅显示没有扩展名的文件名
我的 C# 程序中有以下代码:
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open a file";
fDialog.Filter =
"NCF files (*.ncf)|*.ncf|All files (*.*)|*.*|No Extensions (*.)|*.";
我希望能够让用户从以下选项中进行选择:
*.NCF(仅限具有 .NCF 扩展名的文件)
**.*(所有文件)
和无扩展名的文件,例如:
无扩展名的文件
我知道 ***.* 会执行此操作,但它还会显示 .NCF、.TXT 以及同一目录中的所有其他文件。
我只想能够显示没有扩展名的文件名。
用*过滤。没有成功。在 DOS 窗口 (dir *.) 中执行此操作时效果很好,但 C# 似乎忽略了 *.筛选。
有没有办法用 C# 来做到这一点?
谢谢。
I have the following code in my C# program:
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open a file";
fDialog.Filter =
"NCF files (*.ncf)|*.ncf|All files (*.*)|*.*|No Extensions (*.)|*.";
I want to be able to have the user pick from the following:
*.NCF (files with .NCF extension only)
**.* (all files)
and files that have no extensions such as:
filewithnoextension
I know ***.* will do this, but it also displays the .NCF, .TXT, and all other files in the same directory.
I just want to be able to display filenames that have no extensions.
Filtering with *. does not do the trick. It works fine when doing it with a DOS window (dir *.) , but C# seems to ignore the *. filter.
Is there a way I can do this with C#?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是有效的:
我还没有测试过多个选择。
虽然这是一篇旧文章,但我认为它会对那些寻找一种仅显示没有扩展名的文件的方法的人有益。
I know this works:
I haven't tested with multiple selections..
Altough this is an old post, I though it would benefit someone looking for a way to only display files with no extensions..
自述文件通常有一个扩展名。我想您已经这样做了,但是您是否检查了此文件夹选项以查看已知文件类型的扩展名?它改变了什么吗?
编辑 #1
坦率地说,我怀疑您是否能够制作 OpenFileDialog 显示不带扩展名的文件,如 Filter 属性基于扩展名。
也许您可以使用
System.IO
命名空间对象,例如DirectoryInfo
,这将允许您使用Getfiles()
方法,然后通过 LINQ 过滤自己,仅使用FileInfo.Extension
属性。编辑 #2
由于 OpenFileDialog 是 sealed,您可以将其用作嵌套类型并使用此嵌套类型实现您自己的方法。
我希望这对您有帮助!
A readme file normally has an extension. I suppose you did, but did you check this folder option to see the extensions of known file types? Has it changed anything?
EDIT #1
Frankly, I doubt you'd be able to make the OpenFileDialog display files with no extension, as the Filter property is based on the extension.
Perhaps you could inherit base your own implemented OpenFileDialog using the
System.IO
namespace objects such asDirectoryInfo
, for instance, which will allow you to get the browsed folder files with theGetfiles()
method, then filter yourself through LINQ to display the files with no extension only with theFileInfo.Extension
property.EDIT #2
Since the OpenFileDialog is sealed, you may use it as a nested type and implement your own methods using this nested type.
I hope this helps you!
如果其他软件程序在同一位置创建这些文件,为什么不让您的代码为该文件夹中的每个无扩展名文件添加扩展名(无害的“.XXX”),然后然后显示对话框?
编辑: 或者,请参阅此 MSDN 文章:
http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx
从过滤器部分:
在底部的资源管理器式挂钩过程部分中,本文解释了如何执行此操作。基本上,您将一个事件处理程序传递给 OpenFile 对话框,每次用户导航到新文件夹时,该对话框都会迭代该文件夹中的所有文件,并为每个文件调用事件处理程序。在事件处理程序中,您可以放置代码来确定文件是否具有扩展名,并相应地返回 true 或 false。
If the other software program creates these files in the same location, why not have your code add an extension (something innocuous like ".XXX") to every extension-less file in that folder, and then show the dialog?
Edit: Or, see this MSDN article:
http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx
From the Filters section:
Down at the bottom in the Explorer-Style Hook Procedures section, the article explains how to do this. Basically, you pass an event handler to the OpenFile dialog, and each time the user navigates to a new folder, the dialog iterates through all the files in the folder and calls your event handler for each one. Inside the event handler, you would put your code to determine if the file has an extension or not, and return true or false, accordingly.
我认为使用
*.
可以,但事实并非如此,所以这似乎是OpenFileDialog
控件的限制。您可以创建自己的对话框,但
OpenFileDialog
不可继承,因此对于一个小功能来说,这最终会导致大量工作。没有扩展名的文件是您自己的应用程序创建的吗?如果是这种情况,您可以为其提供自定义扩展以进行过滤。如果不是,那么我恐怕想不出其他任何东西可以帮助你:(
祝你好运!
I thought using
*.
would work, but it doesn't, so it seems that's a limitation of theOpenFileDialog
control.You could create your own dialog but the
OpenFileDialog
is not inheritable so this would end up being a lot of work for just a small feature.Is the file with no extension created by your own application? If that's the case, you could give it a custom extension for your filtering. If it's not, then I'm afraid I can't think of anything else to help you :(
Good luck!