可以在.NET的OpenFileDialog进行设置,让用户选择一个.lnk文件文件、用户、NET、OpenFileDialog

2023-09-03 04:57:04 作者:没心没肺i

我要显示一个对话框,允许用户选择一个快捷方式(.lnk)文件。我的问题是,对话试图获取文件/ URL快捷方式指向而不是.lnk文件本身。

I want to show a dialog that will allow the user to select a shortcut (.lnk) file. My problem is that the dialog tries to get the file/URL the shortcut is pointing to rather then the .lnk file itself.

如何使它允许的.lnk被选中的文件吗?

How can I make it allow .lnk files to be selected?

推荐答案

您可以使用 OpenFileDialog.DereferenceLinks 属性,以影响该行为(see DOC )。

You can use the OpenFileDialog.DereferenceLinks property to influence that behaviour (see doc).

var dlg = new OpenFileDialog();
dlg.FileName = null;
dlg.DereferenceLinks = false;

if (dlg.ShowDialog() == DialogResult.OK) {
    this.label1.Text = dlg.FileName;
}

var dlg = new OpenFileDialog();
dlg.FileName = null; 
this.openFileDialog1.Filter = "Link (*.lnk)|*.lnk";

if (dlg.ShowDialog() == DialogResult.OK) {
    this.label1.Text = dlg.FileName;

这两种方法产生的.lnk 文件,但第一种方法允许的.lnk 文件选择或普通文件,而第二个仅选择的.lnk 文件。

Both methods yield a .lnk file, however the first approach allows the selection of .lnk files or normal files, while the second only selects .lnk files.

 
精彩推荐
图片推荐