Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 Win7 shortcut
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.
 
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 29-Jul-11 4:28am
Edited 29-Jul-11 5:07am
v2
Comments
Philippe Mori at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 

 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();
            }
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 459
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100