Create shortcuts with Visual C # 2005

xiaoxiao2021-04-07  345

Creating shortcuts for the vast majority

Windows users are a dish for a dish, however, this work has brought a lot of trouble to programmers.

.NET does not provide a way to create a shortcut method, then how do we create shortcuts for the application in .NET?

Shortcut file

The shortcut is essentially a file that extends .lnk. Right click on a shortcut file and select Properties, jump to the Shortcut tab, as shown in Figure 1:

You can see a shortcut to include the following data:

· Shortcut name

· The location of the target pointed to by the shortcut

· Work catalog for the goals pointed to by the shortcut

· Activate the hotkey of this shortcut

· Window style (ordinary, maximization and minimization) of target runtime pointed out

· Descriptive text of this shortcut

· The location of the icon where the shortcut

2. Create shortcuts using WSH

2.1 Adding WSH reference

Here I use Visual

C # 2005 Express Edition Beta 2 is developed, add a reference method very simple, right-click your item and select Add a reference, select the COM tab and select Windows Script Host Object Model, as shown in Figure 2:

2.2 Creating your shortcut

Creating a shortcut is as follows:

// Code # 01using System; using IWshRuntimeLibrary; class Program {static void Main (string [] args) {WshShell shell = new WshShell (); IWshShortcut shortcut = (IWshShortcut) shell.CreateShortcut (Environment.GetFoldERPath (Environment.SpecialFolder.DesktopDirectory ) "//" "Allen's Application.lnk"); shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly () Location;. shortcut.WorkingDirectory = System.Environment.CurrentDirectory; shortcut.WindowStyle = 1; shortcut.Description = "Launch Allen's Application"; Shortcut.iconLocation = System.Environment "//" "shell32.dll, 165"; shortcut.save ();}}

First, we create an instance object of WSHSHELL, then create an instance object of the IWSHSHORTCUT interface through the object's CreateShortCut method, and the parameters pass to the CreateShortCut method are the full path to the shortcut to create (including the name of the shortcut). Next, we have to set the relevant attribute values ​​for the IWSHSHORTCUT instance object.

2.3 Setting the properties of shortcuts

2.3.1 TargetPath

This attribute is only used to set or read the location of the target of the shortcut. In Code # 01, the shortcut to create points to this application.

2.3.2 WorkingDirectory

This property specifies the application's work directory, and when the user does not specify a specific directory, the shortcut target application will load or save the file using the directory specified by this property. 2.3.3 WindowStyle

This property specifies that the window of the target application of the shortcut is a normal (original) state, minimizes or maximizes. Comparative item of the Run drop-down menu in Figure 1, the value of this attribute is as follows:

Valuemeaning 1Normal Window 3maximized 7minimized

2.3.4 Description

This attribute sets additional instructions for setting or reading shortcuts.

2.3.5 iconLocation

This attribute is used to specify the location of the chart of the shortcut, and its value contains a complete path and an index value. In Code # 01, the shortcut icon is set as the 165 icon included in the shell32.dll file in the system folder.

2.4 Generating shortcuts

CreateshortCut only creates an instance object of IWshshShortcut, which does not generate any shortcuts, when everything is ready, you must call the IWshshShortcut.save method to generate shortcut files.

3. Simplify operation with fast way

Imagine your application supports the command line parameter combination, for example:

App /out:output.txt / sortby: date / desc

And you hope to use the Ctrl Alt F11 to gently press Ctrl Alt F11, you will need to use the IWshshShortcut's Arguments and HotKey attribute:

// Code # 02shortcut.Arguments = "/out:output.txt / sortby: Date /Desc";shortcut.hotkey =" Ctrl Alt F11 ";

Note: The value of the HotKey property cannot contain spaces.

You can handle command line parameters as usual in the app:

// code # 03class program {static void main (string [] args) {finke.writeline (arg);}}} // output: //// /out:output.txt/ / / sortby: date // / dec

Now, put this shortcut to the desktop, whenever you need to run, gently press CTRL Alt F11, COOL ~ ~ ~

转载请注明原文地址:https://www.9cbs.com/read-132411.html

New Post(0)