C # create shortcuts in pure code mode,

Source: Internet
Author: User
Tags filetime

C # create shortcuts in pure code mode,

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; [financialas (UnmanagedType. byValTStr, SizeConst = MAX_PATH)] string cFileName;} [ComImport] [Guid ("0000010c-0000-0000-c000-000000000046")] [InterfaceType (ComInterfaceType. interfaceIsIUnknown)] public interface IPersist {[PreserveSig] void Ge TClassID (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 ([delealas (UnmanagedType. LPWStr)] string pszFileName, uint dwMode); [PreserveSig] void Save ([financialas (UnmanagedType. L PWStr)] string pszFileName, [financialas (UnmanagedType. bool)] bool fRemember); [PreserveSig] void SaveCompleted ([financialas (UnmanagedType. LPWStr)] string pszFileName); [PreserveSig] void GetCurFile ([financialas (UnmanagedType. LPWStr)] string ppszFileName);} [ComImport] [Guid ("000214f9-0000-0000-c000-0000000000000046")] [InterfaceType (ComInterfaceType. interfaceIsIUnknown)] public interface IShellLink {[Pre ServeSig] void GetPath ([financialas (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 ([exploralas (UnmanagedType. LPWStr, SizeParamIndex = 1)] out string pszName, int cch); [PreserveSig] void SetDescription ([Marshal As (UnmanagedType. LPWStr)] string pszName); [PreserveSig] void GetWorkingDirectory ([financialas (UnmanagedType. LPWStr, SizeParamIndex = 1)] out string pszDir, int cch); [PreserveSig] void SetWorkingDirectory ([financialas (UnmanagedType. LPWStr)] string pszDir); [PreserveSig] void GetArguments ([financialas (UnmanagedType. LPWStr, SizeParamIndex = 1)] out string pszArgs, int cch); [PreserveSig] void SetArgum Ents ([managed Alas (UnmanagedType. LPWStr)] string pszArgs); [PreserveSig] void GetHotkey (out ushort pwHotkey); [PreserveSig] void SetHotkey (ushort wHotkey); [PreserveSig] void GetShowCmd (out int release ); [PreserveSig] void SetShowCmd (int iShowCmd); [PreserveSig] void GetIconLocation ([exploralas (UnmanagedType. LPWStr, SizeParamIndex = 1)] out string pszIconPath, int cch, out int piIcon); [PreserveSi G] void SetIconLocation ([financialas (UnmanagedType. LPWStr)] string pszIconPath, int iIcon); [PreserveSig] void SetRelativePath ([exploralas (UnmanagedType. LPWStr)] string pszPathRel, uint dwReserved); [PreserveSig] void Resolve (IntPtr hwnd, uint fFlags); [PreserveSig] void SetPath ([exploralas (UnmanagedType. LPWStr)] string pszFile);} [GuidAttribute ("00021401-0000-0000-C000-000000000046")] [ClassInte RfaceAttribute (ClassInterfaceType. None)] [ComImportAttribute ()] public class CShellLink {} public const int SW_SHOWNORMAL = 1; // <summary> // create a shortcut. /// </Summary> /// <param name = "shortcutPath"> shortcut path. </Param> /// <param name = "targetPath"> target path. </Param> /// <param name = "workingDirectory"> working path. </Param> /// <param name = "description"> shortcut key 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 (workin GDirectory); 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> /// create a shortcut. /// </Summary> /// <param name = "shortcutPath"> shortcut path. </Param> /// <param name = "targetPath"> target path. </Param> /// <param name = "parameter"> </param> /// <param name = "workingDirectory"> working path. </Param> /// <param name = "description"> shortcut key 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. setArgum Ents (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> /// create a desktop shortcut /// </summary> /// <param name = "targetPath"> executable file path </param>/ // <param name = "description"> shortcut name </param> // <param name = "iconLocation"> shortcut icon path </param> // <param name = "workingDirectory"> working path </param> // <returns> </returns> public static bool createmediatopshortcut (string targetPath, string description, string iconLocation = null, string workingDirector Y = null) {if (string. isNullOrEmpty (workingDirectory) {workingDirectory = MyShortcut. get1_dir ();} return mydomaincut. createShortcut (MyShortcut. get1_dir () + "\" + description + ". lnk ", targetPath, workingDirectory, description, iconLocation );} /// <summary> /// create a shortcut to the program menu /// </summary> /// <param name = "targetPath"> executable file path </param> /// <param name = "description"> shortcut name </param> /// <param nam E = "menuName"> program Menu Sub-menu name, if it is null, no sub-menu is created </param> /// <param name = "iconLocation"> shortcut icon path </param> /// <param name = "workingDirectory"> working path </param> /// <returns> </returns> public static bool CreateProgramsShortcut (string targetPath, string description, string menuName, string iconLocation = null, string workingDirectory = null) {if (string. isNullOrEmpty (workingDirectory) {workingDirectory = MyShortcut. ge TProgramsDir ();} string shortcutPath = MyShortcut. GetProgramsDir (); if (! String. IsNullOrEmpty (menuName) {shortcutPath + = "\" + menuName; if (! System. IO. directory. exists (shortcutPath) {try {System. IO. directory. createDirectory (shortcutPath);} catch // (System. exception ex) {return false ;}} shortcutPath + = "\" + description + ". lnk "; return MyShortcut. createShortcut (shortcutPath, targetPath, workingDirectory, description, iconLocation);} public static bool AddFavorites (string url, string description, string folderName, string icon Location = null, string workingDirectory = null) {if (string. isNullOrEmpty (workingDirectory) {workingDirectory = MyShortcut. getProgramsDir ();} string shortcutPath = MyShortcut. getFavoriteDir (); if (! String. IsNullOrEmpty (folderName) {shortcutPath + = "\" + folderName; if (! System. IO. directory. exists (shortcutPath) {try {System. IO. directory. createDirectory (shortcutPath);} catch // (System. exception ex) {return false ;}} shortcutPath + = "\" + description + ". lnk "; return MyShortcut. createShortcut (shortcutPath, url, workingDirectory, description, iconLocation);} internal const uint SHGFP_TYPE_CURRENT = 0; internal const int MAX_PATH = 260; internal const uint Limit = 0x0016; // All Users \ Start Menu internal const uint CSIDL_COMMON_PROGRAMS = 0x0017; // All Users \ Start Menu \ Programs internal const uint csidl_common_comment topdirectory = 0x0019; // All Users \ Desktop internal const uint CSIDL_PROGRAM_FILES = 0x0026; // C: \ Program Files internal const uint CSIDL_FLAG_CREATE = 0x8000; // new for Win2K, or this in to force creation of folder internal const uint CSIDL_COMMON_FAVORITES = 0x001f; // All Users Favorites [DllImport ("shell32.dll", CharSet = CharSet. unicode, PreserveSig = false)] internal static extern void SHGetFolderPathW (IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, IntPtr pszPath); internal static string SHGetFolderPath) {string pszPath = new string ('', MAX_PATH); IntPtr bstr = Marshal. stringToBSTR (pszPath); SHGetFolderPathW (IntPtr. zero, nFolder, IntPtr. zero, SHGFP_TYPE_CURRENT, bstr); string path = Marshal. ptrToStringBSTR (bstr); int index = path. indexOf ('\ 0'); string path2 = path. substring (0, index); Marshal. freeBSTR (bstr); return path2;} public static string GetSpecialFolderPath (uint csidl) {return SHGetFolderPath (int) (csidl | CSIDL_FLAG_CREATE);} public static string get1_dir () {return GetSpecialFolderPath (optional);} public static string GetProgramsDir () {return GetSpecialFolderPath (optional);} public static string GetFavoriteDir () {return GetSpecialFolderPath (optional );}}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.