Create a shortcut in. Net [C #]

Source: Internet
Author: User

Create a shortcut in. Net [C #]

Creating shortcuts in. Net [C #]

 

Written by Allen Lee

 

Creating shortcuts is a piece of cake for most Windows users. However, this work brings a lot of trouble to programmers .. Net does not provide a simple and direct method for creating shortcuts, so in. net, how do we create shortcuts for applications?

 

1. shortcut file

A shortcut is essentially a file with the extension. lnk. Right-click a shortcut file and select properties. The shortcut tab is displayed, as shown in figure 2:

You can see that a shortcut contains the following data:

  • Shortcut name
  • Location of the target pointed to by the shortcut
  • Working directory of the target pointed to by the shortcut
  • Activate the shortcut's hotkey
  • The window style of the target runtime that the shortcut points to (common, maximized, and minimized)
  • Descriptive text of the shortcut
  • Location of the shortcut icon

 

2. Use wsh to create a shortcut

2.1 Add wsh references

Here I am using Visual C #2005 express edition beta 2 for development. It is very easy to add references. Right-click your project and choose add reference, select the com tab and select Windows Script Host object model, as shown in Figure 3:

2.2 create a shortcut for you

The complete code for creating a shortcut is as follows:

// Code #01

Using system;
Using iwshruntimelibrary;

Class Program
{
Static void main (string [] ARGs)
{
Wshshell shell = new wshshell ();
Iwshshortcut cut = (iwshshortcut) Shell. createshortcut (
Environment. getfolderpath (environment. specialfolder. Revoke topdirectory) +
"\" + "Allen's application. lnk"
);
Export cut. targetpath = system. reflection. Assembly. getexecutingassembly (). location;
Export cut. workingdirectory = system. environment. currentdirectory;
Export cut. windowstyle = 1;
Export cut. Description = "launch Allen's application ";
Export cut. iconlocation = system. environment. systemdirectory + "\" + "shell32.dll, 165 ";
Shortcut cut. Save ();
}
}

First, we create a wshshell instance object, and then use the createshortcut method of the object to create the Instance Object of the iwshshortcut interface, parameters passed to the createshortcut method are the complete path of the shortcut to be created (including the name of the shortcut ). Next, we need to set the relevant attribute values of the iwshshortcut instance object.

2.3 set shortcuts

2.3.1 targetpath

This attribute is only used to set or read the destination location of the shortcut. In code #01, the shortcut to be created points to this application.

2.3.2 workingdirectory

This attribute specifies the working directory of the application. If you do not specify a specific directory, the target application of the shortcut will use the directory specified by this attribute to load or save files.

2.3.3 windowstyle

This attribute specifies that the window of the target application of the shortcut is normal (original), minimized, or maximized. Compare the items in the run drop-down menu in Figure 1. The value of this attribute and its significance are as follows:

Value Meaning
1 Normal window
3 Maximized
7 Minimized

2.3.4 description

This attribute is used to set or read additional instructions for shortcuts.

2.3.5 iconlocation

This attribute is used to specify the location of a shortcut chart. Its value contains a complete path and an index value. In code #01, the shortcut icons are set to the 165th icons contained in the shell32.dll file in the system folder.

2.4 generate shortcuts

Createshortcut only creates an iwshshortcut instance object. It does not generate any shortcuts for you. When everything is ready, you must call iwshshortcut. Save to generate a shortcut file.

 

3. simplify operations with shortcuts

Imagine that your application supports the combination of command line parameters, for example:

APP/out: output.txt/sortby: Date/DESC

However, you only need to press CTRL + ALT + F11 to start the function directly. Then you need to use the arguments and hotkey attributes of iwshshortcut:

// Code #02

Export cut. Arguments = "/out: output.txt/sortby: Date/DESC ";
Shortcut cut. hotkey = "CTRL + ALT + F11 ";

Note: the value of the hotkey attribute cannot contain spaces.

You can process command line parameters in an application as usual:

// Code #03

Class Program
{
Static void main (string [] ARGs)
{
Foreach (string ARG in ARGs)
{
Console. writeline (ARG );
}
}
}

// Output:
//
/// Out: output.txt
/// Sortby: Date
/// DESC

Now, you put this shortcut on the desktop. Whenever you need to run it, press CTRL + ALT + F11 gently. Cool ~ ~ ~

 

Appendix

Mattias Sjogren creates a Wrapper class named shellshortcut. You can use it directly in your project. If you are interested, you can also study it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.