OpenFileDialog - 仅显示没有扩展名的文件名

发布于 2024-09-01 17:51:51 字数 628 浏览 6 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反话 2024-09-08 17:51:52

我知道这是有效的:

fDialog.Filter = "No extension Files|" + null;

我还没有测试过多个选择。

虽然这是一篇旧文章,但我认为它会对那些寻找一种仅显示没有扩展名的文件的方法的人有益。

I know this works:

fDialog.Filter = "No extension Files|" + null;

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..

山色无中 2024-09-08 17:51:52

自述文件通常有一个扩展名。我想您已经这样做了,但是您是否检查了此文件夹选项以查看已知文件类型的扩展名?它改变了什么吗?

编辑 #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 as DirectoryInfo, for instance, which will allow you to get the browsed folder files with the Getfiles() method, then filter yourself through LINQ to display the files with no extension only with the FileInfo.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!

死开点丶别碍眼 2024-09-08 17:51:52

如果其他软件程序在同一位置创建这些文件,为什么不让您的代码为该文件夹中的每个无扩展名文件添加扩展名(无害的“.XXX”),然后然后显示对话框?

编辑: 或者,请参阅此 MSDN 文章:

http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx

过滤器部分:

CDN_INCLUDEITEM 通知
消息提供了另一种过滤方式
对话框中显示的名称
显示。要使用此消息,请提供
OFNHookProc 挂钩过程和
指定 OFN_ENABLEINCLUDENOTIFY
OPENFILENAME 结构中的标志
当您创建对话框时。每个
当用户打开文件夹时,
对话框发送 CDN_INCLUDEITEM
通知你的钩子程序
对于新打开的每个项目
文件夹。钩子的返回值
procedure 指示对话框是否
框中应显示该项目
文件夹的项目列表。

在底部的资源管理器式挂钩过程部分中,本文解释了如何执行此操作。基本上,您将一个事件处理程序传递给 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:

The CDN_INCLUDEITEM notification
message provides another way to filter
the names that the dialog box
displays. To use this message, provide
an OFNHookProc hook procedure and
specify the OFN_ENABLEINCLUDENOTIFY
flag in the OPENFILENAME structure
when you create the dialog box. Each
time the user opens a folder, the
dialog box sends a CDN_INCLUDEITEM
notification to your hook procedure
for each item in the newly opened
folder. The return value of the hook
procedure indicates whether the dialog
box should display the item in the
folder's item list.

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.

朕就是辣么酷 2024-09-08 17:51:52

我认为使用 *. 可以,但事实并非如此,所以这似乎是 OpenFileDialog 控件的限制。

您可以创建自己的对话框,但 OpenFileDialog 不可继承,因此对于一个小功能来说,这最终会导致大量工作。

没有扩展名的文件是您自己的应用程序创建的吗?如果是这种情况,您可以为其提供自定义扩展以进行过滤。如果不是,那么我恐怕想不出其他任何东西可以帮助你:(

祝你好运!

I thought using *. would work, but it doesn't, so it seems that's a limitation of the OpenFileDialog 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文