C# 创建快捷方式,如建立桌面和程序夹的快捷方式链接,并且上ALL USERS级别

废话没什么可以说的,就是要注意引一个COM :  wshom.ocx

 

需要的朋友使用下面的代码:

using System;
using System.Collections.Generic;
using System.Text;


using System.Runtime.InteropServices;
using System.IO;
using IWshRuntimeLibrary;
using System.Windows.Forms;

namespace Sipek.UserControls
{
  

    /// <summary>
    /// http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx  CSIDL on MSDN
    /// </summary>
    public enum SpecialFolderType : int
    {

        CSIDL_DESKTOP = 0x000,
        CSIDL_INTERNET = 0x001,
        CSIDL_PROGRAMS = 0x002,
        CSIDL_CONTROLS = 0x003,
        CSIDL_PRINTERS = 0x004,
        CSIDL_PERSONAL = 0x005,
        CSIDL_FAVORITES = 0x006,
        CSIDL_STARTUP = 0x007,
        CSIDL_RECENT = 0x008,
        CSIDL_SENDTO = 0x009,
        CSIDL_BITBUCKET = 0x00A,
        CSIDL_STARTMENU = 0x00B,
        CSIDL_MYDOCUMENTS = 0x00C,
        CSIDL_MYMUSIC = 0x00D,
        CSIDL_MYVIDEO = 0x00E,
        CSIDL_DESKTOPDIRECTORY = 0x0010,
        CSIDL_DRIVES = 0x0011,
        CSIDL_NETWORK = 0x0012,
        CSIDL_NETHOOD = 0x0013,
        CSIDL_FONTS = 0x0014,
        CSIDL_TEMPLATES = 0x0015,
        CSIDL_COMMON_STARTMENU = 0x0016,
        CSIDL_COMMON_PROGRAMS = 0x0017,
        CSIDL_COMMON_STARTUP = 0x0018,
        CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019,
        CSIDL_APPDATA = 0x001A,
        CSIDL_PRINTHOOD = 0x001B,
        CSIDL_LOCAL_APPDATA = 0x001C,
        CSIDL_ALTSTARTUP = 0x001D,
        CSIDL_COMMON_ALTSTARTUP = 0x001E,
        CSIDL_COMMON_FAVORITES = 0x001F,
        CSIDL_INTERNET_CACHE = 0x0020,
        CSIDL_COOKIES = 0x0021,
        CSIDL_HISTORY = 0x0022,
        CSIDL_COMMON_APPDATA = 0x0023,
        CSIDL_WINDOWS = 0x0024,
        CSIDL_SYSTEM = 0x0025,
        CSIDL_PROGRAM_FILES = 0x0026,
        CSIDL_MYPICTURES = 0x0027,
        CSIDL_PROFILE = 0x0028,
        CSIDL_SYSTEMX86 = 0x0029,
        CSIDL_PROGRAM_FILESX86 = 0x002A,
        CSIDL_PROGRAM_FILES_COMMON = 0x002B,
        CSIDL_PROGRAM_FILES_COMMONX86 = 0x002C,
        CSIDL_COMMON_TEMPLATES = 0x002D,
        CSIDL_COMMON_DOCUMENTS = 0x002E,
        CSIDL_COMMON_ADMINTOOLS = 0x002F,
        CSIDL_ADMINTOOLS = 0x0030,
        CSIDL_CONNECTIONS = 0x0031,
        CSIDL_COMMON_MUSIC = 0x0035,
        CSIDL_COMMON_PICTURES = 0x0036,
        CSIDL_COMMON_VIDEO = 0x0037,
        CSIDL_RESOURCES = 0x0038,
        CSIDL_RESOURCES_LOCALIZED = 0x0039,
        CSIDL_COMMON_OEM_LINKS = 0x003A,
        CSIDL_CDBURN_AREA = 0x003B,
        CSIDL_COMPUTERSNEARME = 0x003D,
        CSIDL_FLAG_PER_USER_INIT = 0x00800,
        CSIDL_FLAG_NO_ALIAS = 0x001000,
        CSIDL_FLAG_DONT_VERIFY = 0x004000,
        CSIDL_FLAG_CREATE = 0x008000,
        CSIDL_FLAG_MASK = 0x00FF00,

    }


    /// <summary>
    /// Summary description for Link.
    /// </summary>
    public class ShortCutsLink
    {

        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
           uint dwFlags, [Out] StringBuilder pszPath);
        public static string Common_SpecialFolder_Path(SpecialFolderType SpecialFolder)
        {
            StringBuilder sbPath = new StringBuilder(512);
            int CSIDL = (int)SpecialFolder;
            SHGetFolderPath(IntPtr.Zero, CSIDL, IntPtr.Zero, 0, sbPath);
            return sbPath.ToString();
        }
        /// <summary>
        /// Check to see if a shortcut exists in a given directory with a specified file name
        /// </summary>
        /// <param name="DirectoryPath">The directory in which to look</param>
        /// <param name="FullPathName">The name of the shortcut (without the .lnk extension) or the full path to a file of the same name</param>
        /// <returns>Returns true if the link exists</returns>
        public static bool Exists(string DirectoryPath, string LinkPathName)
        {
            // Get some file and directory information
            DirectoryInfo SpecialDir = new DirectoryInfo(DirectoryPath);
            // First get the filename for the original file and create a new file
            // name for a link in the Startup directory
            //
            FileInfo originalfile = new FileInfo(LinkPathName);
            string NewFileName = SpecialDir.FullName + "//" + originalfile.Name + ".lnk";
            FileInfo linkfile = new FileInfo(NewFileName);
            return linkfile.Exists;
        }

        //Check to see if a shell link exists to the given path in the specified special folder
        // return true if it exists
        public static bool Exists(Environment.SpecialFolder folder, string LinkPathName)
        {
            return ShortCutsLink.Exists(Environment.GetFolderPath(folder), LinkPathName);
        }


        /// <summary>
        /// Update the specified folder by creating or deleting a Shell Link if necessary
        /// </summary>
        /// <param name="folder">A SpecialFolder in which the link will reside</param>
        /// <param name="TargetPathName">The path name of the target file for the link</param>
        /// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored.</param>
        /// <param name="Create">If true, create the link, otherwise delete it</param>
        public static void Update(Environment.SpecialFolder folder, string TargetPathName, string LinkPathName, bool install)
        {
            // Get some file and directory information
            ShortCutsLink.Update(Environment.GetFolderPath(folder), TargetPathName, LinkPathName, install);
        }

        // boolean variable "install" determines whether the link should be there or not.
        // Update the folder by creating or deleting the link as required.

        /// <summary>
        /// Update the specified folder by creating or deleting a Shell Link if necessary
        /// </summary>
        /// <param name="DirectoryPath">The full path of the directory in which the link will reside</param>
        /// <param name="TargetPathName">The path name of the target file for the link</param>
        /// <param name="LinkPathName">The file name for the link itself or, if a path name the directory information will be ignored. like "ACDSee"</param>
        /// <param name="Create">If true, create the link, otherwise delete it</param>
        public static void Update(string DirectoryPath, string TargetPathName, string LinkPathName, bool Create)
        {
            // Get some file and directory information
            DirectoryInfo SpecialDir = new DirectoryInfo(DirectoryPath);
            // First get the filename for the original file and create a new file
            // name for a link in the Startup directory
            //
            FileInfo OriginalFile = new FileInfo(LinkPathName);
            string NewFileName = SpecialDir.FullName + "//" + OriginalFile.Name + ".lnk";
            FileInfo LinkFile = new FileInfo(NewFileName);

            if (Create) // If the link doesn't exist, create it
            {
                if (LinkFile.Exists) return; // We're all done if it already exists
                //Place a shortcut to the file in the special folder
                try
                {
                    // Create a shortcut in the special folder for the file
                    // Making use of the Windows Scripting Host
                    WshShell shell = new WshShell();
                    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkFile.FullName);
                    link.TargetPath = TargetPathName;
                    link.Save();
                }
                catch
                {
                    MessageBox.Show("Unable to create link in special directory: " + NewFileName,
                        "Shell Link Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else // otherwise delete it from the startup directory
            {
                if (!LinkFile.Exists) return; // It doesn't exist so we are done!
                try
                {
                    LinkFile.Delete();
                }
                catch
                {
                    MessageBox.Show("Error deleting link in special directory: " + NewFileName,
                        "Shell Link Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }

}
//**************************************************************************************

 

//如何使用:

    public class ShortCut
    {
        public static string AssemblyProduct
        {
            get
            {
                // 获取此程序集上的所有 Product 属性
                object[] attributes = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), false);
                // 如果 Product 属性不存在,则返回一个空字符串
                if (attributes.Length == 0)
                    return "USB Phone";
                // 如果有 Product 属性,则返回该属性的值
                return ((System.Reflection.AssemblyProductAttribute)attributes[0]).Product;
            }
        }
        public static void CreateShortCut()
        {
            //if (!ShortCutsLink.Exists(ShortCutsLink.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_PROGRAMS), "CMS-LAN"))
            //{
            //    DialogResult addshortcut = MessageBox.Show(
            //        "是否为所有用户添加 CMS-LAN 的快捷方式到 开始--程序菜单?", "添加快捷方式到...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //    if (addshortcut == DialogResult.Yes)
            //    {
            //        Link.Update(Link.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_PROGRAMS), Application.ExecutablePath, "CMS-LAN", true);//开始--》程序
            //    }
            //}

            //if (!ShortCutsLink.Exists(ShortCutsLink.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_DESKTOPDIRECTORY), AssemblyProduct))
            //{
            //    DialogResult addshortcut = MessageBox.Show(
            //        "是否为所有用户添加 CMS-LAN 的快捷方式到 桌面?", "添加快捷方式到...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //    if (addshortcut == DialogResult.Yes)
            //    {
            //        ShortCutsLink.Update(Link.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_DESKTOPDIRECTORY), Application.ExecutablePath, "CMS-LAN", true);//All User/s桌面
            //    }
            //}
            ShortCutsLink.Update(ShortCutsLink.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_PROGRAMS), System.Windows.Forms.Application.ExecutablePath, AssemblyProduct, true);//开始--》程序
            ShortCutsLink.Update(ShortCutsLink.Common_SpecialFolder_Path(SpecialFolderType.CSIDL_COMMON_DESKTOPDIRECTORY), System.Windows.Forms.Application.ExecutablePath, AssemblyProduct, true);//All User/s桌面
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值