WinForm Create Shortcut

the using the System;
 the using the System.IO;
 the using IWshRuntimeLibrary; 

namespace WUtility.IOHelper 
{ 

    // reference to the Host IWshRuntimeLibrary COM Object the Model Script component -Windows 

    ///  <Summary> 
    /// to create a shortcut class
     ///  </ Summary> 
    / //  <Remarks> </ Remarks> 
    public  class ShortCutHelper 
    { 
        // int the WindowStyle DESCRIPTION
         @ 1 activates and displays the window. If the window is minimized or maximized, the system will restore to its original size and position.
        // 3 Activates the window and displays it as a maximized window.
        // 7 Minimizes the window and activates the next top-level window. 

        ///  <Summary> 
        ///Create Shortcut
         ///  </ the Summary> 
        ///  <param name = "Directory"> shortcut in which the folder </ param> 
        ///  <param name = "ShortcutName"> Shortcut Name </ param> 
        ///  <param name = "The targetPath"> destination path </ param> 
        ///  <param name = "description"> description </ param> 
        ///  <param name = "IconLocation"> icon path, the format of " executable or DLL path, the icon ID ",
         /// e.g. + System.Environment.SystemDirectory" \\ "+" Shell32.dll, 165 " </ param> 
        ///  <Remarks> </ Remarks>
        public static void CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
        {
            if (!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }

            string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut) shell.CreateShortcut (shortcutPath); // create a shortcut object 
            shortcut.TargetPath = The targetPath; // targeting path 
            shortcut.WorkingDirectory = Path.GetDirectoryName (The targetPath); // set the starting position 
            shortcut.WindowStyle = . 1 ; // set the operation mode, the default is conventional window 
            shortcut.Description = the Description; // setting Remarks 
            shortcut.IconLocation = String .IsNullOrEmpty (IconLocation) targetPath:? IconLocation; // set the icon path may not be assigned, the default is the target icon 
            shortcut. the save (); // save the shortcut 
        } 

        ///  <Summary>
        // create a desktop shortcut
         ///  </ the Summary> 
        ///  <param name = "ShortcutName"> Shortcut Name </ param> 
        ///  <param name = "targetPath"> target path </ param> 
        / //  <param name = "description"> description </ param> 
        ///  <param name = "IconLocation"> icon path, the format "path to an executable file or the DLL, the icon ID" </ param> 
        ///  < Remarks> </ Remarks> 
        public  static  void CreateShortcutOnDesktop ( String ShortcutName, String The targetPath,string description = null, string iconLocation = null) 
        { 
            String Desktop = Environment.GetFolderPath (Environment.SpecialFolder.DesktopDirectory); // Get desktop folder path of 
            the CreateShortcut (Desktop, ShortcutName, The targetPath, Description, IconLocation); 
        } 

    } 
}

 

Examples of Use

        private void button1_Click(object sender, EventArgs e)
        {
            ShortCutHelper.CreateShortcutOnDesktop("快捷方式名称", "D:\\Chrome.exe");
        }

 

Guess you like

Origin www.cnblogs.com/aisa-thanatos/p/10949975.html
Recommended