sari k
sari k

Reputation: 2111

How can I create a 'power options' shortcut on desktop with C# code?

How can I create a shortcut of power options with C# code?

This is my code:

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

Upvotes: 1

Views: 649

Answers (2)

Moo-Juice
Moo-Juice

Reputation: 38820

The items in the control panel are stored in C:\windows\system32 with a .cpl extension. Therefore your target, for power options, should be:

C:\windows\system32\powercfg.cpl

Note: Do not use the hard-coded strings I have used here, use Environment.SystemDirectory to locate the directory appropriately.

Upvotes: 1

Tony
Tony

Reputation: 10347

Building on the answer from Moo-Juice, try the following:

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

Upvotes: 2

Related Questions