5

I'm trying to access the target path from a shortcut(.lnk) file on a 64bit machine using my 32 bit application. To get the path, I was using the following code.

WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(flexShortcut);
FLEXEXE = link.TargetPath;

This returns the path C:\Program Files (X86)\.. instead of C:\Program Files\..

Is there any work around for this?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Jimesh
  • 113
  • 6
  • 2
    Since your app is 32 bit i think it will always return in this fashion – V4Vendetta Aug 19 '11 at 11:03
  • 1
    Check this out http://stackoverflow.com/questions/2284725/how-do-get-the-path-of-program-files-regardless-of-the-architecture-of-the-target – V4Vendetta Aug 19 '11 at 11:09
  • Thanks, but changing the build platform didn't work. I was hoping a work around as the shortcut points to a 64bit application in C:\Program Files\... – Jimesh Aug 19 '11 at 11:41
  • You are *creating* a shortcut. It is up to you to *set* the TargetPath. Post code that actually reproduces the problem. – Hans Passant Aug 19 '11 at 12:59
  • @hans - when using the `WshRuntimeLibrary`, you call `shell.CreateShortcut()` to read the `.lnk` file properties as well. Counter intuitive I know, but that's how it works. I repro'd myself with an existing shortcut on Win7 x64 where the shortcut points to `c:\program files`. – Kev Aug 19 '11 at 13:29
  • 1
    Ah, okay. Hmm, painful, Wow64DisableWow64FsRedirection() doesn't fix this. Setting the platform target to AnyCPU certainly does. – Hans Passant Aug 19 '11 at 14:23
  • 1
    I tried setting the platform target to AnyCPU. It hasn't fixed the problem... – Jimesh Aug 21 '11 at 19:11

3 Answers3

2

A trick to be used is to

  1. Get the file name from link.TargetPath
  2. Get the working directory from link.WorkingDirectory
  3. Append path
Rand Random
  • 7,300
  • 10
  • 40
  • 88
Jimesh
  • 113
  • 6
0

For anyone still having this issue, I found that leaving the Platform Target set to "Any CPU" and unticking the option "Prefer 32-bit" fixed it for me. This way your application will maintain the flexibility of being compatible with both architectures, and still return the shortcut link TargetPath (either x86 / x64) correctly.

Boost3d1
  • 1
  • 1
0

This is a general problem in Windows. Try editing a file in a System32 folder (e.g. applicationHost.config) with a 32-bit editor. You will be editing the file from the SysWOW64 folder instead... It's more than strange...

L. Alina
  • 13
  • 6
Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55