Search code examples
c#wpfopenfiledialog

How to read all data shortcut using OpenFileDialog?


I'm making a launcher to open all applications on my computer. But I do not know how to read the parameters of the opened file is a shortcut. I have tried using:

openFileDialog.DereferenceLinks = false; //and true

Can anyone help me? My code is here:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog od = new OpenFileDialog();
    od.DereferenceLinks = false;
    od.Multiselect = false;
    od.SupportMultiDottedExtensions = true;

    if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if (System.IO.Path.GetExtension(od.FileName).ToLower().Equals(".lnk"))
        {
            MessageBox.Show(//xxxxxxx how to sho the parameter?); for example output c:\\.....\hl.exe -a -b -c -d -e 29332
        }
    }
}

Solution

  • I can't understand what the problem is here. You said you've already discovered the FileDialog.DereferenceLinks property, which does exactly what you want.

    When it is set to true, the dialog box dereferences all shortcuts, returning the path of the item that they point to, rather than the path of the shortcut file itself. Only when it is set to false will you get files with a .lnk extension returned from the dialog.

    So the code that you have just added to the question is wrong (or at least, makes things much more difficult for you than they need to be). It should look more like this:

    OpenFileDialog od = new OpenFileDialog();
    od.DereferenceLinks = true;  // set this to true if you want the actual file
    od.Multiselect = false;
    od.SupportMultiDottedExtensions = true;
    
    if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        // You will never get a path to a shortcut file (*.lnk) here.
        Debug.Assert(!String.Equals(System.IO.Path.GetExtension(od.FileName),
                                    ".lnk",
                                    StringComparison.OrdinalIgnoreCase));
    
        // ... do something with the file
    }
    

    Otherwise, dereferencing shortcut files takes a fair bit of effort. You do so using the IShellLink COM interface, which I don't believe is wrapped explicitly by any part of the .NET BCL. You'll need to write the code to use it yourself. I can't imagine why you'd need to in this case.

    That is what you'll have to do if you need to read the arguments from a shortcut file.

    1. Set the OpenFileDialog.DereferenceLinks property to false so that you get shortcut files returned.
    2. You probably also want to set the OpenFileDialog.Filter property to Shortcut files (*.lnk)|*.lnk in order to ensure that the user can only select shortcut files in the dialog.
    3. Once the user has selected a shortcut file, create an IShellLink object for that file.
    4. If that succeeds, use the GetPath method to obtain a string containing the path and file name of the shortcut file, and GetArguments method to obtain a string containing the command-line arguments associated with that shortcut file.
    5. Finally, append the arguments string to the end of the path string.

    You can either write the wrapper code to use the IShellLink COM interface from .NET yourself, search online to find one that's already been written (no guarantees about its quality, though), or add a reference to the ShellLinkObject class which is designed for scripting but still usable from .NET.