Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to programmatically change a short cut target on XP and Windows 7.

I have code that works in Windows XP using IWshShell/IWshShortcut.

It also appears to work in Windows 7, that is, I run the program to modify the short cut and then right click on the icon/short cut and check properties and it shows the short cut target has been changed.

Then when I click on the short cut, it reverts back to the target of the short cut before it was changed??

I suspect this is a security issue related to Windows 7, but I am Admin on my system.

Any ideas would be appreciated.

C#
public bool UpdateTarget(string shortCutPathFile, string newTargetPathFile,string hotKey )
{
    FileInfo LinkFile = new FileInfo(shortCutPathFile);
    if (LinkFile.Exists)
    {
        WshShell shell = new WshShell();
        IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullName);
        string targetPath = link.TargetPath;

        if( hotKey != null; && hotKey.Length > 0 && hotKey != string.Empty )
        {
            link.Hotkey = hotKey;
        }

        link.TargetPath = newTargetPathFile;
        link.Description = "Start App";
        link.Save();
        return (true);
    }
    return (false);
}


Thanks.
Posted
Updated 29-Jul-11 4:07am
v2
Comments
Philippe Mori 29-Jul-11 19:46pm    
I would think that it is more an auto-repair. Depending on how an application is installed, some auto-repair operations are automatically done by the system. In fact, most of the informations on how an application was installed are kept with the system so that the system could perform some "auto-repair" or maintenance task to help applications keep working as expected.

System are far less fragile now that it used to be a few OS version back.

1 solution

I developed the same program on windows XP sp2 x86 using VS2005 and then tested it on Windows 7 x64. But it worked fine for me. Try this and reply if this is working for you.


C#
private void btnChangeShortcut_Click(object sender, EventArgs e)
       {
           //textBox2 contains new file path
           UpdateTarget(shortCutPathFile, textBox2.Text);
       }



       public void UpdateTarget(string shortCutPathFile, string newTargetPathFile)
       {
           WshShell shell = new WshShell();
           FileInfo LinkFile = new FileInfo(shortCutPathFile);
           if (LinkFile.Exists)
           {
               IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullName);
               string targetPath = link.TargetPath;
               link.TargetPath = newTargetPathFile;
               link.Description = "Start App";
               link.Save();
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900