C # are two ways to create a shortcut

One: Create a shortcut directly with WSH:
1. First you want to add a reference.
Add a reference method is very simple, right-click your project and select Add Reference,
select the COM tab and select Script Host Object Model Windows
2. reference a namespace
using System.Runtime.InteropServices; // interactive services
a using IWshRuntimeLibrary;

3. create shortcut (comment contains a detailed description)
// instantiate an object WshShell
WshShell shell = new new WshShell ();

// create IWshShortcut by the method of the object CreateShortcut examples of interface objects
IWshShortcut shortcut = (IWshShortcut) shell.CreateShortcut (
    Environment.GetFolderPath (Environment.SpecialFolder.Desktop) + "//ShortCut.lnk");

a position shortcut // set the destination is located (the source full path)
. shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly () Location;

working directory application //
// when the user does not specify a particular directory, the target application using the shortcut attribute specified directory load or save the file.
= System.Environment.CurrentDirectory shortcut.WorkingDirectory;

// target application window type (1.Normal window normal window, 3.Maximized maximized window, 7.Minimized minimized)
shortcut.WindowStyle =. 1;

Description // shortcut
= shortcut.Description "ChinaDforce YanMang";

. // You can customize the shortcut icon (if not set, it will default to the source file icon)
//shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32 .dll, 165 ";

startup parameters // set the application (if the application supports it)
//shortcut.Arguments =" / myWord / D4S ";

// set the shortcut keys (if necessary.)
// shortcut. = Hotkey "CTRL + ALT + D";

// save the shortcut
shortcut.Save ();


disadvantages:
write in this way, you must have Interop.IWshRuntimeLibrary.dll follow,
. the right to perform for the creation of "one-file program "the people, the trouble of it.


Two: By creating VBS, and execute, created:
1. First, look at the VBS code that creates a shortcut:
'VBS instance
the SET WshShell = WScript.CreateObject ( "WScript.Shell")
strDesktop = WshShell.SpecialFolders ( "Desktop")' get the desktop directory
set oShellLink = WshShell.CreateShortcut (strDesktop & "\ D4S.lnk") 'shortcut storage directory and name
oShellLink.TargetPath = "X: \ Program files \ XXX.exe"' pointing to the executable file
oShellLink.WindowStyle = 1 'operating mode (form opens the way )
oShellLink.Hotkey = "the SHIFT + CTRL + F" 'shortcuts
oShellLink.IconLocation = "X: \ Program Files \ XXX.exe, 0"' icon (the same may designate)
oShellLink.Description = "ChinaDforce YanMang" 'information Memo
oShellLink.WorkingDirectory = "X: \ Program Files \" ' home directory
oShellLink.Save' saving shortcuts

2.
? How do we use it in C # VBS way I think there should be a lot of it!
Here introduce a "most stupid" but the most direct way.
Idea is as follows:
>>> VBS generate all the code text;
>>> write temporary files "temp.vbs";
>>> open the file execution Process.

3. The following is the key to the code implemented in C #:
// VBS code is generated
string vbs this.CreateVBS = ();
// file format written in the temporary folder
this.WriteToTemp (VBS);
// call Process execution
this.RunProcess ();

// the code to generate VBS
String this.CreateVBS VBS = ();
/ / written in the form of temporary file folder
this.WriteToTemp (VBS);
// call Process execution
this.RunProcess ();
///
/// create VBS Code
///
///
Private String CreateVBS ()
{
    String VBS string.Empty =;

    VBS = + ( "SET WScript.CreateObject the WshShell = (\" WScript.Shell \ ") \ R & lt \ n-");
    VBS = + ( "= strDesktop the 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");

    return vbs;
}
///
/// 写入临时文件
///
///
private void WriteToTemp(string vbs)
{
    if (!string.IsNullOrEmpty (VBS))         // temporary files
    {

        TEMPFILE Environment.GetFolderPath = String (Environment.SpecialFolder.Templates) + "[URL = File: // \\ temp.vbs] \\ temp.vbs [/ URL]";
        // file write
        FileStream fs = new FileStream ( TEMPFILE, FileMode.Create, FileAccess.Write);
        the try
        {
            // use herein must UnicodeEncoding as ASCII or UTF-8 VBS cause distortion.
            System.Text.UnicodeEncoding new new UNI UnicodeEncoding = ();
            byte [] B = UNI. the GetBytes (VBS);
            fs.Write (B, 0, b.length);
            fs.Flush ();
            fs.Close ();
        }
        the catch (Exception EX)
        {
            MessageBox.Show (ex.Message, "write temporary files There was an error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        The finally
        {
            // release resources
            fs.Dispose ();
        }
    }
}
///
codes in VBS execution ///
///
Private void RunProcess ()
{
    String TEMPFILE = Environment.GetFolderPath (Environment.SpecialFolder.Templates) + "\\ temp.vbs";
    IF (the File.Exists (TEMPFILE))
    {
        // perform the VBS
        the Process.Start (TEMPFILE);
    }
}
Private void BTN exit _Click (SENDER Object, EventArgs E)
{
    Application.Exit () ;
    // clear temporary files
    File.Delete (Environment.GetFolderPath (Environment.SpecialFolder.Templates) + " \\ temp.vbs");
}

Emphasize that:
when writing VBS file, be sure to use UnicodeEncoding .
Because UTF-8 and ASCII code, can lead to shortcuts when VBS is generated,
garbled, leading to shortcuts wrong.
I had to use UTF8Encoding time, not placed in the path contains Chinese can be, but a Chinese appear hung up!
bothers me the details quite a while, only to find.

 

 

Source: https://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html

One: Create a shortcut directly with WSH:
1. First you want to add a reference.
Add a reference method is very simple, right-click your project and select Add Reference,
select the COM tab and select Script Host Object Model Windows
2. reference a namespace
using System.Runtime.InteropServices; // interactive services
a using IWshRuntimeLibrary;

3. create shortcut (comment contains a detailed description)
// instantiate an object WshShell
WshShell shell = new new WshShell ();

// create IWshShortcut by the method of the object CreateShortcut examples of interface objects
IWshShortcut shortcut = (IWshShortcut) shell.CreateShortcut (
    Environment.GetFolderPath (Environment.SpecialFolder.Desktop) + "//ShortCut.lnk");

a position shortcut // set the destination is located (the source full path)
. shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly () Location;

working directory application //
// when the user does not specify a particular directory, the target application using the shortcut attribute specified directory load or save the file.
= System.Environment.CurrentDirectory shortcut.WorkingDirectory;

// target application window type (1.Normal window normal window, 3.Maximized maximized window, 7.Minimized minimized)
shortcut.WindowStyle =. 1;

Description // shortcut
= shortcut.Description "ChinaDforce YanMang";

. // You can customize the shortcut icon (if not set, it will default to the source file icon)
//shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32 .dll, 165 ";

startup parameters // set the application (if the application supports it)
//shortcut.Arguments =" / myWord / D4S ";

// set the shortcut keys (if necessary.)
// shortcut. = Hotkey "CTRL + ALT + D";

// save the shortcut
shortcut.Save ();


disadvantages:
write in this way, you must have Interop.IWshRuntimeLibrary.dll follow,
. the right to perform for the creation of "one-file program "the people, the trouble of it.


Two: By creating VBS, and execute, created:
1. First, look at the VBS code that creates a shortcut:
'VBS instance
the SET WshShell = WScript.CreateObject ( "WScript.Shell")
strDesktop = WshShell.SpecialFolders ( "Desktop")' get the desktop directory
set oShellLink = WshShell.CreateShortcut (strDesktop & "\ D4S.lnk") 'shortcut storage directory and name
oShellLink.TargetPath = "X: \ Program files \ XXX.exe"' pointing to the executable file
oShellLink.WindowStyle = 1 'operating mode (form opens the way )
oShellLink.Hotkey = "the SHIFT + CTRL + F" 'shortcuts
oShellLink.IconLocation = "X: \ Program Files \ XXX.exe, 0"' icon (the same may designate)
oShellLink.Description = "ChinaDforce YanMang" 'information Memo
oShellLink.WorkingDirectory = "X: \ Program Files \" ' home directory
oShellLink.Save' saving shortcuts

2.
? How do we use it in C # VBS way I think there should be a lot of it!
Here introduce a "most stupid" but the most direct way.
Idea is as follows:
>>> VBS generate all the code text;
>>> write temporary files "temp.vbs";
>>> open the file execution Process.

3. The following is the key to the code implemented in C #:
// VBS code is generated
string vbs this.CreateVBS = ();
// file format written in the temporary folder
this.WriteToTemp (VBS);
// call Process execution
this.RunProcess ();

// the code to generate VBS
String this.CreateVBS VBS = ();
/ / written in the form of temporary file folder
this.WriteToTemp (VBS);
// call Process execution
this.RunProcess ();
///
/// create VBS Code
///
///
Private String CreateVBS ()
{
    String VBS string.Empty =;

    VBS = + ( "SET WScript.CreateObject the WshShell = (\" WScript.Shell \ ") \ R & lt \ n-");
    VBS = + ( "= strDesktop the 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");

    return vbs;
}
///
/// 写入临时文件
///
///
private void WriteToTemp(string vbs)
{
    if (!string.IsNullOrEmpty (VBS))         // temporary files
    {

        TEMPFILE Environment.GetFolderPath = String (Environment.SpecialFolder.Templates) + "[URL = File: // \\ temp.vbs] \\ temp.vbs [/ URL]";
        // file write
        FileStream fs = new FileStream ( TEMPFILE, FileMode.Create, FileAccess.Write);
        the try
        {
            // use herein must UnicodeEncoding as ASCII or UTF-8 VBS cause distortion.
            System.Text.UnicodeEncoding new new UNI UnicodeEncoding = ();
            byte [] B = UNI. the GetBytes (VBS);
            fs.Write (B, 0, b.length);
            fs.Flush ();
            fs.Close ();
        }
        the catch (Exception EX)
        {
            MessageBox.Show (ex.Message, "write temporary files There was an error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        The finally
        {
            // release resources
            fs.Dispose ();
        }
    }
}
///
codes in VBS execution ///
///
Private void RunProcess ()
{
    String TEMPFILE = Environment.GetFolderPath (Environment.SpecialFolder.Templates) + "\\ temp.vbs";
    IF (the File.Exists (TEMPFILE))
    {
        // perform the VBS
        the Process.Start (TEMPFILE);
    }
}
Private void BTN exit _Click (SENDER Object, EventArgs E)
{
    Application.Exit () ;
    // clear temporary files
    File.Delete (Environment.GetFolderPath (Environment.SpecialFolder.Templates) + " \\ temp.vbs");
}

Emphasize that:
when writing VBS file, be sure to use UnicodeEncoding .
Because UTF-8 and ASCII code, can lead to shortcuts when VBS is generated,
garbled, leading to shortcuts wrong.
I had to use UTF8Encoding time, not placed in the path contains Chinese can be, but a Chinese appear hung up!
bothers me the details quite a while, only to find.

Guess you like

Origin www.cnblogs.com/mq0036/p/12118184.html