如何在C#中的OpenFileDialog中限制文件名修改 [英] How to restrict filename modification in OpenFileDialog in C#

查看:306
本文介绍了如何在C#中的OpenFileDialog中限制文件名修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.NET 3.5程序,该程序使用OpenFileDialog来检索要导入的文件.它具有两个过滤器(*Domain*.**.*),并且选择*Domain*.*作为FilterIndex.在OpenFileDialog中修改文件时,我发现过滤器中的文件消失了.

I have a C# .NET 3.5 program that uses an OpenFileDialog to retrieve a file for importing. It has two filters (*Domain*.* and *.*) and the *Domain*.* is chosen as the FilterIndex. I'm seeing a case of a disappearing file from the filters when it is modified within the OpenFileDialog.

以下是用户将要重现该问题的步骤:

Here are the steps the user will take to reproduce the problem:

  1. 在程序中,打开OpenFileDialog.确保将过滤器设置为*Domain*.*.
  2. 在目录中找到与过滤器匹配的文件,并更改文件名,使其不再与过滤器匹配. 注意:文件将消失.
  3. 更改过滤器以显示所有文件*.*. 注意:即使您正在显示*.*,该文件也未列出.
  1. In the program, open the OpenFileDialog. Ensure that the filter is set to *Domain*.*.
  2. Find a file in the directory that matches the filter and change the file name so that it no longer matches the filter. Note: the file will disappear.
  3. Change the filter to show all files *.*. Note: the file is not listed even though you're supposedly showing *.*.

这时,您可以在Windows资源管理器中打开该文件夹,然后查看存在具有更改名称的文件.但是,在OpenFileDialog中,无论使用什么过滤器,它都不再可见.再次查看该文件的唯一方法是关闭OpenFileDialog,然后重新打开它.

At this point, you can open the folder in Windows Explorer and see that the file with the changed name exists. However, in the OpenFileDialog, it is no longer visible regardless of the filter that is used. The only way to see that file again is to close the OpenFileDialog and then reopen it.

这是我创建OpenFileDialog的代码.

using(OpenFileDialog domainFileDialog = new OpenFileDialog())
{
    domainFileDialog.CheckFileExists = true;
    domainFileDialog.CheckPathExists = true;
    domainFileDialog.Filter = "Domain Files (*Domain*.*)|*Domain*.*|All files (*.*)|*.*";
    domainFileDialog.FilterIndex = 1;
    domainFileDialog.Multiselect = false;
    domainFileDialog.RestoreDirectory = true;
    domainFileDialog.ShowReadOnly = true;
    domainFileDialog.SupportMultiDottedExtensions = true;
    domainFileDialog.Title = "Choose the Domain File to Import...";
    domainFileDialog.ValidateNames = true;

    \\ Perform Import functionality...
}

似乎当您打开OpenFileDialog并修改与限制性更强的过滤器匹配的现有文件,使其不再与过滤器匹配时,即使显示了所有文件*.*,该文件也会从可见性中消失.

It seems as though when you open the OpenFileDialog and modify an existing file that matches the more restrictive filter so that it no longer matches the filter, the file disappears from visibility even when all files *.* are displayed.

由于我不知道为什么会发生这种情况(并且我很乐意提出解决问题的建议)是否有任何方法可以限制用户在以下位置创建或修改OpenFileDialog中的任何文件:希望预防这个问题?

Since I have no idea why this is happening, (and I'm open for suggestions as to how to fix it) is there any way to restrict a user from creating or modifying any of the files in the OpenFileDialog in hopes of preventing this problem?

推荐答案

首先,我建议您考虑是否值得这样做.如果FOD这样做,则每个Windows应用程序都具有此功能,但是您不会听到用户抱怨它.那么这真的是一个需要解决的问题吗?

First, I'd suggest you consider whether it is worth caring that the user might do this. If the FOD does this, every windows application has this feature, but you don't hear users complaining about it. So is it really an issue that needs fixing?

如果问题与过滤器所涉及的不仅仅是文件扩展名,这也不会令我感到惊讶-这样做非常规.也许只尝试使用扩展名,看看是否是原因.

It wouldn't surprise me if the problem's related to the filter being for more than just a file extension - it's rather unconventional to do this. Maybe try it with an extension only and see if this is the cause.

您是否尝试过按F5刷新其缓存的数据?我不知道这是否可以像在资源管理器窗口中那样在FOD中起作用,但是尝试一下可能会很有趣.

Have you tried pressing F5 to refresh its cached data? I don't know if this would work in a FOD as it does in an explorer window, but it might be interesting to give it a try.

话虽如此,过滤按键(F2)和鼠标单击(右键)是微不足道的(使用某些形式您可以派生它,或者您可以放入MessageFilter)以使其不可能用户可以访问允许其重命名FOD中的文件的任何UI.如果您以这种方式破坏了FOD,那么作为用户,这会让我感到恼火-无论如何,这比在极少数情况下消失"的重命名文件还重要.

Having said all that, it would be trivial to filter keypresses (F2) and mouse clicks (right button) )(with some forms you could derive off it, or you could drop in a MessageFilter) to make it impossible for a user to access any UI that allows them to rename a file in the FOD. It would piss me off, as a user, if you broke the FOD in this waym though - much more than a renamed file "disappearing" under rare circumstances, anyway.

这篇关于如何在C#中的OpenFileDialog中限制文件名修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆