0

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
        }
    }
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
user2277061
  • 7
  • 1
  • 4
  • Please be more descriptive on what you want to achieve. Also, post what you have got until now. When you select a shortcut, the path of the file will return. Do you have shortcuts defined with parameters and you don't know how to read the parameters set in the shortcuts ? – Alexandru Dicu Jul 09 '13 at 10:09
  • Show your code, what we are suposed to do? Write all code for you? :) Show some invention – Kamil Budziewski Jul 09 '13 at 10:11
  • @alexandrudicu i need the full path of the target filename and the parameter (arguments) to opening the application – user2277061 Jul 09 '13 at 10:35
  • @wudzik I hace attached my line codes, :) im sorry – user2277061 Jul 09 '13 at 10:38

1 Answers1

2

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • In my case, the output should be like this **"c:\.....\app.exe -a -b -c -d -e 234324"** – user2277061 Jul 09 '13 at 10:48
  • @user2277061 Updated my answer. I still don't know *why* you need that. But it's possible. – Cody Gray - on strike Jul 10 '13 at 03:15
  • Thank you for your solution. I'm building a small app as a launcher that will make a list of all the applications on my computer. And all of the data that will be stored into a sqlite database. This database will store the location of program execution and all the arguments required to open the application. For example, "-cs-noforce hl.exe" to execute Counter Strike game. – user2277061 Jul 10 '13 at 11:52