如何使用C#代码在桌面上创建“电源选项”快捷方式?

时间:2010-11-18 10:46:13

标签: c#

如何使用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();

2 个答案:

答案 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正确定位目录。