create a shortcut directly with WSH:
1. First add a reference.
The way to add a reference is very simple, right-click your project and choose Add Reference,
Select the COM tab and select Windows Script Host Object Model
2. Referencing namespaces
using System.Runtime.InteropServices; // using
3. Create a shortcut (detailed in the note)
//instantiating a WshShell objectWshShell Shell =NewWshShell ();//use the object's CreateShortcut method to create an instance object of the Iwshshortcut interfaceIwshshortcut shortcut =(iwshshortcut) shell. CreateShortcut (Environment.getfolderpath (Environment.SpecialFolder.Desktop)+"//shortcut.lnk"); //set the target location of the shortcut (source program full path)Shortcut. TargetPath =System.Reflection.Assembly.GetExecutingAssembly (). Location; //working directory of the application//When a user does not specify a specific directory, the shortcut's target application uses the directory specified by the property to mount or save the file. Shortcut. WorkingDirectory =System.Environment.CurrentDirectory;//Target application window type (1.Normal window normal windows, 3.Maximized maximized window, 7.Minimized minimized)Shortcut. WindowStyle =1; //Description of the shortcutShortcut. Description ="Chinadforce Yanmang"; //You can customize the shortcut icon. (If not set, the default source file icon.) //shortcut. IconLocation = System.Environment.SystemDirectory + "\ \" + "shell32.dll, 165"; //Set startup parameters for the application (if supported by the application)//shortcut. Arguments = "/myword/d4s"; //set shortcut keys (if necessary.)//shortcut. Hotkey = "Ctrl+alt+d"; //Save ShortcutShortcut. Save ();
Disadvantages:
The program written in this way must have Interop.IWshRuntimeLibrary.dll followed,
To execute correctly. For people who create "single-file programs," It's a problem.
by creating a VBS, and executing, creating the way:
1. First look at the code for VBS to create a shortcut:
'VBS instanceSetWshShell = WScript.CreateObject ("Wscript.Shell") Strdesktop= Wshshell.specialfolders ("Desktop")'get the Desktop directorySetOshelllink = Wshshell.createshortcut (Strdesktop &"\d4s.lnk")'Shortcut Store directory and nameOshelllink.targetpath ="X:\Program Files\xxx.exe" 'A pointer to an executable fileOshelllink.windowstyle =1 'How to run (form opens)Oshelllink.hotkey ="ctrl+shift+f" 'shortcut KeysOshelllink.iconlocation ="X:\Program files\xxx.exe, 0" 'icon (also not specified)Oshelllink.description ="Chinadforce Yanmang" 'Notes InformationOshelllink.workingdirectory ="X:\Program files\" 'Start DirectoryOshelllink.save'Save Shortcut
2. How do we use VBS in C #?
Method I think there should be a lot of it!
Here is a "dumbest" but the most straightforward way to do this.
Ideas are as follows:
>>> generate VBS all code text;
>>> Write temporary file "Temp.vbs";
>>> Open this file with Process execution.
3. The following are the key codes implemented in C #:
//Generate VBS CodestringVBS = This. Createvbs (); //write a temporary folder as a file This. WRITETOTEMP (VBS); //Call Process Execution This. Runprocess (); //Generate VBS CodestringVBS = This. Createvbs (); //write a temporary folder as a file This. WRITETOTEMP (VBS); //Call Process Execution This. Runprocess (); /// ///Create VBS code/// /// Private stringCreatevbs () {stringVBS =string. Empty; VBS+= ("Set WshShell = WScript.CreateObject (\ "Wscript.shell\") \ r \ n"); VBS+= ("strdesktop = wshshell.specialfolders (\ "Desktop\") \ r \ n"); VBS+= ("Set oshelllink = Wshshell.createshortcut (Strdesktop & \ "\\D4S.lnk\") \ r \ n"); VBS+= ("Oshelllink.targetpath = \ ""+ System.Reflection.Assembly.GetExecutingAssembly (). Location +"\ "\ r \ n"); VBS+= ("Oshelllink.windowstyle = 1\r\n"); VBS+= ("oshelllink.description = \ "Chinadforce yanmang\" \ r \ n"); VBS+= ("oshelllink.workingdirectory = \ ""+ System.Environment.CurrentDirectory +"\ "\ r \ n"); VBS+= ("Oshelllink.save"); returnVBS;} /// ///Write temporary files/// /// Private voidWritetotemp (stringVBS) { if(!string. IsNullOrEmpty (VBS)) {//Temporary Files stringTempfile = Environment.getfolderpath (Environment.SpecialFolder.Templates) +"[Url=file://\\temp.vbs]\\temp.vbs[/url]"; //Write FileFileStream fs =NewFileStream (Tempfile, FileMode.Create, FileAccess.Write); Try { //unicodeencoding must be used here. Because VBS is garbled with UTF-8 or ASCIISystem.Text.UnicodeEncoding Uni =Newunicodeencoding (); byte[] B =Uni. GetBytes (VBS); Fs. Write (b,0, b.length); Fs. Flush (); Fs. Close (); } Catch(Exception ex) {MessageBox.Show (ex). Message,"An error occurred while writing to the temp file", MessageBoxButtons.OK, Messageboxicon.error); } finally { //Freeing ResourcesFS. Dispose (); } } } /// ///executing the code in the VBS/// Private voidrunprocess () {stringTempfile = Environment.getfolderpath (Environment.SpecialFolder.Templates) +"\\temp.vbs"; if(File.exists (tempfile)) {//Execute VBSProcess.Start (tempfile); } } Private voidBTN Exit _click (Objectsender, EventArgs e) {application.exit (); //Clear Temporary FilesFile.delete (Environment.getfolderpath (Environment.SpecialFolder.Templates) +"\\temp.vbs"); }
Emphasize one thing:
Be sure to use unicodeencoding when writing to the VBS file.
Because the UTF-8 and ASCII code will cause VBS to generate shortcuts,
garbled, resulting in a shortcut error.
I originally use UTF8Encoding, do not put in the path containing Chinese can also, but the appearance of Chinese will hang!
Troubled me long time, only to find this detail.
Source: http://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html
C # Create a shortcut