문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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;
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top