Windows 7以编程方式在桌面上添加/更改快捷方式 [英] Windows 7 programmatically add/change short cut on desk top
问题描述
我正在尝试以编程方式更改XP和Windows 7上的快捷目标.
我有使用IWshShell/IWshShortcut在Windows XP中运行的代码.
它似乎也可以在Windows 7中运行,也就是说,我运行程序来修改快捷方式,然后右键单击图标/快捷方式并检查属性,这表明快捷方式目标已更改.
然后,当我单击快捷方式时,它会还原为更改之前的快捷方式的目标??
我怀疑这是与Windows 7有关的安全问题,但我是系统上的管理员.
任何想法将不胜感激.
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.
推荐答案
我使用VS2005在 windows XP sp2 x86 上开发了相同的程序,然后在 Windows 7 x64 .但这对我来说很好.尝试此操作,然后回复是否适合您.
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();
}
}
这篇关于Windows 7以编程方式在桌面上添加/更改快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!