OpenFileDialog 继续显示 .lnk 文件 WPF

OpenFileDialog keep showing .lnk files WPF

是否可以强制 OpenFileDialog 不显示快捷方式?

我只想获取pdf,所以我使用这样的过滤器

var dialog = new OpenFileDialog
{
   Multiselect = false,
   Filter = "Pdf Files|*.pdf"
};

但是当显示对话框时,它显示的文件具有 pdf 扩展名 lnk 扩展名

是否可以预防?

即在保存对话框中,我将它用于 excel 个文件。

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

所以,跟打开文件对话框应该是一样的。

您不想显示 .lnk 文件的原因是因为您想要实际 PDF 文件的路径吗?如果是这样,您应该能够将 DereferenceLinks 属性 设置为 true。如果用户选择 .lnk 文件,对话框将 return .lnk 文件指向的文件路径,而不是 .lnk 文件本身。

The answer is from the MSDN forums

开箱即用的 OpenFileDialog 无法做到这一点。 一个原因可能是,用户可以使用 .lnk 文件导航到不同的文件夹,其中 he/she 期望需要打开的文件。

在上面发布的 link 中,用户 'Ryan' 发布了一个片段,以排除在 FileOK 事件中选择的 .lnk 文件。

再说一次,不是我的代码!但是由于一些网站移动了他们的内容并且 link 可能不再有效,这里 'Ryan's 的代码片段(写在 VB):

Public Class Home

    Private WithEvents _fileDialog As New OpenFileDialog

    Private Sub BrowseButton1_Click(sender As Object, e As EventArgs) Handles BrowseButton1.Click
        With Me._fileDialog
            .DereferenceLinks = True    ' this allows the FileName property to have the Target of a shortcut link, instead of the shortcut link file name
            .Multiselect = False
            .Filter = "CSV (Comma delimited) (*.csv)|*.csv"
            .ShowDialog()
        End With
    End Sub

    Private Sub _fileDialog_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles _fileDialog.FileOk
        If Not Me._fileDialog.FileName Like "*.csv" Then
            ' cancel any shortcut files here
            e.Cancel = True
            MsgBox("You must select a CSV (Comma delimited) file.", MsgBoxStyle.Exclamation)
        Else
            Me.TextBox1.Text = Me._fileDialog.FileName
        End If
    End Sub

End Class

您不能创建一个打开文件对话框来隐藏特定文件。您唯一可以做的就是使用 Filter 标签来过滤指定的文件扩展名以保存而不是隐藏。