17.7 Create a Shortcut on the Desktop or Start Menu


Problem

You need to create a shortcut on the user 's desktop or start menu.

Solution

Use COM interop to access the functionality of the Windows Script Host. Create and configure an IWshShortcut instance that represents the shortcut. The folder in which you save the shortcut determines whether it appears on the desktop or start menus .

Discussion

The .NET Framework class library doesn't include the functionality to create desktop or start menu shortcuts; however, this is relatively easy to do using the Windows Script Host component accessed through COM Interop. Recipe 15.6 details how to create an interop assembly that provides access to a COM component. If you're using Visual Studio .NET, add a reference to the Windows Script Host Object Model listed in the COM tab of the Add Reference dialog. If you don't have Visual Studio .NET, use the Type Library Importer (Tlbimp.exe) to create an interop assembly for the wshom.ocx file, which is usually located in the Windows\System32 folder. (You can obtain the latest version of the Windows Script Host from http://msdn.microsoft.com/scripting . At the time of this writing, the latest version was 5.6.)

Once you have generated and imported the interop assembly into your project, follow these steps to create a desktop or start menu shortcut.

  1. Instantiate a WshShell object, which provides access to the Windows shell.

  2. Use the SpecialFolders property of the WshShell object to determine the correct path of the folder where you want to put the shortcut. You must specify the name of the folder that you want as an index to the SpecialFolders property. For example, to create a desktop shortcut, specify the value Desktop , and to create a start menu shortcut, specify StartMenu . Using the SpecialFolders property, you can obtain the path to any of the special system folders; other commonly used values include AllUsersDesktop and AllUsersStartMenu .

  3. Call the CreateShortcut method of the WshShell object, and provide the fully qualified file name of the shortcut file you want to create. The file should have the extension .lnk . CreateShortcut will return an IWshShortcut instance.

  4. Use the properties of the IWshShortcut instance to configure the shortcut. You can configure properties such as the executable that the shortcut references, a description for the shortcut, a hotkey sequence, and the icon displayed for the shortcut.

  5. Call the Save method of the IWshShortcut instance to write the shortcut to disk. The shortcut will appear either on the desktop or in the start menu (or elsewhere) depending on the path specified when the IWshShortcut instance was created.

The ShortcutExample class creates a shortcut to Notepad.exe on both the desktop and start menu of the current user. ShortcutExample creates both shortcuts by calling the CreateShortcut method and specifying a different destination folder for each shortcut file. This approach makes it possible to create the shortcut file in any of the special folders returned by the WshShell.SpecialFolders property. Here is the ShortcutExample code:

 using System; using IWshRuntimeLibrary; public class ShortcutExample {     public static void Main() {         // Create the Notepad shortcut on the Desktop.         CreateShortcut("Desktop");         // Create the Notepad shortcut on the Windows Start menu of          // the current user.         CreateShortcut("StartMenu");         // Wait to continue.         Console.WriteLine("Main method complete. Press Enter.");         Console.ReadLine();     }     public static void CreateShortcut(string destination) {         // Create a WshShell instance through which to access the          // functionality of the Windows shell.         WshShell wshShell = new WshShell();         // Assemble a fully qualified name that places the Notepad.lnk         // file in the specified destination folder. You could use the          // System.Environment.GetFolderPath method to obtain a path, but          // the WshShell.SpecialFolders method provides access to a wider          // range of folders. You need to create a temporary object reference          // to the destination string to satisfy the requirements of the          // Item method.         object destFolder = (object)destination;         string fileName =              (string)wshShell.SpecialFolders.Item(ref destFolder)              + @"\Notepad.lnk";         // Create the shortcut object. Nothing is created in the          // destination folder until the shortcut is saved.         IWshShortcut shortcut =              (IWshShortcut)wshShell.CreateShortcut(fileName);         // Configure the fully qualified name to the executable.         // Use the Environment class for simplicity.         shortcut.TargetPath =              Environment.GetFolderPath(Environment.SpecialFolder.System)              + @"\notepad.exe";         // Set the working directory to the Personal (My Documents) folder.         shortcut.WorkingDirectory =             Environment.GetFolderPath(Environment.SpecialFolder.Personal);         // Provide a description for the shortcut.         shortcut.Description = "Notepad Text Editor";         // Assign a hotkey to the shortcut.         shortcut.Hotkey = "CTRL+ALT+N";         // Configure Notepad to always start maximized.         shortcut.WindowStyle = 3;         // Configure the shortcut to display the first icon in notepad.exe.         shortcut.IconLocation = "notepad.exe, 0";          // Save the configured shortcut file.         shortcut.Save();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net