国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c# 純代碼方式創建快捷方式

2019-11-17 02:27:08
字體:
來源:轉載
供稿:網友
c# 純代碼方式創建快捷方式
using System;using System.Collections.Generic;using System.Text;using Microsoft.Win32;using System.Runtime.InteropServices;namespace ShortcutNamespace{    class MyShortcut    {        [StructLayout(LayoutKind.Sequential)]        public struct FILETIME        {            uint dwLowDateTime;            uint dwHighDateTime;        }        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]        public struct WIN32_FIND_DATA        {            public const int MAX_PATH = 260;            uint dwFileAttributes;            FILETIME ftCreationTime;            FILETIME ftLastaccessTime;            FILETIME ftLastWriteTime;            uint nFileSizeHight;            uint nFileSizeLow;            uint dwOID;            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]            string cFileName;        }        [ComImport]        [Guid("0000010c-0000-0000-c000-000000000046")]        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]        public interface ipersist        {            [PReserveSig]            void GetClassID(out Guid pClassID);        }        [ComImport]        [Guid("0000010b-0000-0000-C000-000000000046")]        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]        public interface IPersistFile            : IPersist        {            new void GetClassID(out Guid pClassID);            [PreserveSig]            int IsDirty();            [PreserveSig]            void Load(                [MarshalAs(UnmanagedType.LPWStr)] string pszFileName,                uint dwMode);            [PreserveSig]            void Save(                [MarshalAs(UnmanagedType.LPWStr)] string pszFileName,                [MarshalAs(UnmanagedType.Bool)] bool fRemember);            [PreserveSig]            void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);            [PreserveSig]            void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);        }        [ComImport]        [Guid("000214F9-0000-0000-C000-000000000046")]        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]        public interface IShellLink        {            [PreserveSig]            void GetPath(                [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszFile,                int cch,                ref WIN32_FIND_DATA pfd,                uint fFlags);            [PreserveSig]            void GetIDList(out IntPtr ppidl);            [PreserveSig]            void SetIDList(IntPtr ppidl);            [PreserveSig]            void GetDescription(                [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszName,                int cch);            [PreserveSig]            void SetDescription(                [MarshalAs(UnmanagedType.LPWStr)] string pszName);            [PreserveSig]            void GetWorkingDirectory(                [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszDir,                int cch);            [PreserveSig]            void SetWorkingDirectory(                [MarshalAs(UnmanagedType.LPWStr)] string pszDir);            [PreserveSig]            void GetArguments(                [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszArgs,                int cch);            [PreserveSig]            void SetArguments(                [MarshalAs(UnmanagedType.LPWStr)] string pszArgs);            [PreserveSig]            void GetHotkey(out ushort pwHotkey);            [PreserveSig]            void SetHotkey(ushort wHotkey);            [PreserveSig]            void GetShowCmd(out int piShowCmd);            [PreserveSig]            void SetShowCmd(int iShowCmd);            [PreserveSig]            void GetIconLocation(                [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszIconPath,                int cch,                out int piIcon);            [PreserveSig]            void SetIconLocation(                [MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,                int iIcon);            [PreserveSig]            void SetRelativePath(                [MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,                uint dwReserved);            [PreserveSig]            void Resolve(                IntPtr hwnd,                uint fFlags);            [PreserveSig]            void SetPath(                [MarshalAs(UnmanagedType.LPWStr)] string pszFile);        }        [GuidAttribute("00021401-0000-0000-C000-000000000046")]        [ClassInterfaceAttribute(ClassInterfaceType.None)]        [ComImportAttribute()]        public class CShellLink        {        }        public const int SW_SHOWNORMAL = 1;        /// <summary>        /// 創建快捷方式。        /// </summary>        /// <param name="shortcutPath">快捷方式路徑。</param>        /// <param name="targetPath">目標路徑。</param>        /// <param name="workingDirectory">工作路徑。</param>        /// <param name="description">快捷鍵描述。</param>        public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null)        {            try            {                CShellLink cShellLink = new CShellLink();                IShellLink iShellLink = (IShellLink)cShellLink;                iShellLink.SetDescription(description);                iShellLink.SetShowCmd(SW_SHOWNORMAL);                iShellLink.SetPath(targetPath);                             iShellLink.SetWorkingDirectory(workingDirectory);                if (!string.IsNullOrEmpty(iconLocation))                {                    iShellLink.SetIconLocation(iconLocation, 0);                }                            IPersistFile iPersistFile = (IPersistFile)iShellLink;                iPersistFile.Save(shortcutPath, false);                Marshal.ReleaseComObject(iPersistFile);                iPersistFile = null;                Marshal.ReleaseComObject(iShellLink);                iShellLink = null;                Marshal.ReleaseComObject(cShellLink);                cShellLink = null;                return true;            }            catch //(System.Exception ex)            {                return false;            }        }        /// <summary>        /// 創建快捷方式。        /// </summary>        /// <param name="shortcutPath">快捷方式路徑。</param>        /// <param name="targetPath">目標路徑。</param>        /// <param name="parameter"></param>        /// <param name="workingDirectory">工作路徑。</param>        /// <param name="description">快捷鍵描述。</param>                public static bool CreateShortcut(string shortcutPath, string targetPath, string parameter, string workingDirectory, string description, string iconLocation = null)        {            try            {                CShellLink cShellLink = new CShellLink();                IShellLink iShellLink = (IShellLink)cShellLink;                iShellLink.SetDescription(description);                iShellLink.SetShowCmd(SW_SHOWNORMAL);                iShellLink.SetPath(targetPath);                iShellLink.SetArguments(parameter);                iShellLink.SetWorkingDirectory(workingDirectory);                               if (!string.IsNullOrEmpty(iconLocation))                {                    iShellLink.SetIconLocation(iconLocation, 0);                }                IPersistFile iPersistFile = (IPersistFile)iShellLink;                iPersistFile.Save(shortcutPath, false);                Marshal.ReleaseComObject(iPersistFile);                iPersistFile = null;                Marshal.ReleaseComObject(iShellLink);                iShellLink = null;                Marshal.ReleaseComObject(cShellLink);                cShellLink = null;                return true;            }            catch //(System.Exception ex)            {                return false;            }        }        /// <summary>        /// 創建桌面快捷方式        /// </summary>        /// <param name="targetPath">可執行文件路徑</param>        /// <param name="description">快捷方式名稱</param>
上一篇:譯文---C#堆VS棧(Part Four)

下一篇:LINQ基礎

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 榕江县| 鄄城县| 比如县| 黑山县| 甘泉县| 广昌县| 涞源县| 南溪县| 宝丰县| 农安县| 鞍山市| 桂平市| 阜阳市| 布尔津县| 呈贡县| 汾西县| 安福县| 若羌县| 夏津县| 武隆县| 工布江达县| 崇义县| 萨迦县| 兰州市| 新宁县| 盖州市| 抚宁县| 宁陵县| 南宫市| 海城市| 凤台县| 新源县| 玉环县| 鄱阳县| 仙居县| 翁牛特旗| 深圳市| 宜君县| 富川| 蒙山县| 大冶市|