如何使用C#代码创建电源选项的快捷方式?
这是我的代码:
WshShellClass wshShell = new WshShellClass();
IWshRuntimeLibrary.IWshShortcut MyShortcut;
MyShortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(@"C:\user\Administrator\power options.lnk");
MyShortcut.TargetPath = ???????;
MyShortcut.IconLocation = Application.StartupPath + ??????;
MyShortcut.Save();
答案 0 :(得分:2)
根据Moo-Juice的回答,尝试以下方法:
WshShell shell = new WshShell();
string app = Path.Combine(Environment.SystemDirectory, "powercfg.cpl");
string linkPath = @"C:\PowerLink.lnk";
IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(linkPath);
link.TargetPath = app;
link.IconLocation = string.Format("{0},2", app);
link.Save();
答案 1 :(得分:1)
控制台中的项目存储在C:\windows\system32
中,扩展名为.cpl
。因此,对于电源选项,您的目标应该是:
C:\windows\system32\powercfg.cpl
注意:请勿使用我在此处使用的硬编码字符串,使用Environment.SystemDirectory
正确定位目录。