.NET OpenFileDialog 是否可以设置为允许用户选择 .lnk 文件

发布于 2024-08-23 09:15:33 字数 102 浏览 4 评论 0原文

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

如何让它允许选择 .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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧城空念 2024-08-30 09:15:33

您可以使用 OpenFileDialog.DereferenceLinks 属性来影响该行为 (参见文档)。

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文件。

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;
}

or

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

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

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.

谁的新欢旧爱 2024-08-30 09:15:33

以下代码为我返回了一个 .lnk 文件名

  public static string PromptForOpenFilename (Control parent)
  {
     OpenFileDialog dlg = new OpenFileDialog ();

     dlg.Filter = "Link (*.lnk)|*.lnk";
     dlg.Multiselect = false;
     dlg.FileName = null;

     DialogResult res;
     if (null != parent)
        res = dlg.ShowDialog (parent);
     else
        res = dlg.ShowDialog ();

     if (DialogResult.OK == res)
        return dlg.FileName;
     return null;
  }

The following code returned a .lnk filename for me

  public static string PromptForOpenFilename (Control parent)
  {
     OpenFileDialog dlg = new OpenFileDialog ();

     dlg.Filter = "Link (*.lnk)|*.lnk";
     dlg.Multiselect = false;
     dlg.FileName = null;

     DialogResult res;
     if (null != parent)
        res = dlg.ShowDialog (parent);
     else
        res = dlg.ShowDialog ();

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