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>
/// <param name="iconLocation">快捷方式图标路径</param>
/// <param name="workingDirectory">工作路径</param>
/// <returns></returns>
public static bool CreateDesktopShortcut(string targetPath, string description, string iconLocation = null, string workingDirectory = null)
{
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = MyShortcut.GetDeskDir();
}
return MyShortcut.CreateShortcut(MyShortcut.GetDeskDir() + "\\" + description + ".lnk", targetPath, workingDirectory, description, iconLocation);
} /// <summary>
/// 创建程序菜单快捷方式
/// </summary>
/// <param name="targetPath">可执行文件路径</param>
/// <param name="description">快捷方式名称</param>
/// <param name="menuName">程序菜单中子菜单名称,为空则不创建子菜单</param>
/// <param name="iconLocation">快捷方式图标路径</param>
/// <param name="workingDirectory">工作路径</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.GetProgramsDir();
}
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 iconLocation = 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 CSIDL_COMMON_STARTMENU = 0x0016; // All Users\Start Menu
internal const uint CSIDL_COMMON_PROGRAMS = 0x0017; // All Users\Start Menu\Programs
internal const uint CSIDL_COMMON_DESKTOPDIRECTORY = 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(int nFolder)
{
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 GetDeskDir()
{
return GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
} public static string GetProgramsDir()
{
return GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);
} public static string GetFavoriteDir()
{
return GetSpecialFolderPath(CSIDL_COMMON_FAVORITES);
}
}
}

  

c# 纯代码方式创建快捷方式的更多相关文章

  1. 【iOS开发】多屏尺的自动适配 AutoLayout (纯代码方式)

    关于AutoLayout,最早从iOS6开始引入使用.   主要功能是使用约束,对视图进行相对布局,以适应不同屏尺的变换.   网上大量的资料都在介绍xib和storyboard,如何使用AutoLa ...

  2. 通过纯代码方式发布WCF服务

    网络上搜索WCF服务,一般是寄宿在IIS,通过WebConfig方式配服务地址,接口类型等信息,但是对于我这样的懒人,目前项目在开发阶段,实在不愿意每次添加新服务就更新配置文件,于是使用了反射来加载服 ...

  3. (纯代码)快速创建wcf rest 服务

    因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBind ...

  4. 使用fastreport以代码方式创建报表

    Report report = new Report();// register the "Products" tablereport.RegisterData(dataSet1. ...

  5. Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局

    简介 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints. 项目主页: Masonry 最新示例: 点击下载 项目简议: 如果再看到关于纯代 ...

  6. Masonry — 使用纯代码进行iOS应用的autolayout自适应布局

    本文转载至   http://www.ios122.com/2015/09/masonry/ 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstrain ...

  7. iOS OC纯代码企业级项目实战之我的云音乐(持续更新))

    简介 这是一个使用OC语言,从0使用纯代码方式开发一个iOS平台,接近企业级商业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识:主要是讲解如何使用系统功能,流行的第三方 ...

  8. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

  9. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

随机推荐

  1. linux 文件权限除了r、w、x外还有s、t、i、a权限:

    s:文件属主和组设置SUID和GUID,文件在被设置了s权限后将以root身份执行.在设置s权限时文件属主.属组必须先设置相应的x权限,否 则s权限并不能正真生效(c h m o d命令不进行必要的完 ...

  2. BNUOJ 1038 Flowers

    春天到了,师大的园丁们又开始忙碌起来了. 京师广场上有一块空地,边界围成了一个多边形,内部被划分成一格一格的.园丁们想在这个多边形内的每一格内种植一些花. 现在请你帮忙计算一下一共最多可以种多少花. ...

  3. Python Django 的 django templatedoesnotexist

    django 1.8版本的解决方案 在  setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], //  这个 列表里添加 template路 ...

  4. How to install OpenResty

    How to install OpenResty 15 January 2014, 6:18 am   OpenResty, also called “ngx_openresty”, is a web ...

  5. MYSQL集群的搭建

    按照此配置完全可以配置成功!! 一.介绍========测试环境:Server1:ndbd 192.168.1.225Server2:ndbd 192.168.1.226Server3:mysqld ...

  6. MPlayer-ww 增加边看边剪切功能

    解压到 D:\MPlayer-ww 运行 copy_font.bat 安装字体 LED_font.ttf 双击 MPlayer_ww_openWith.reg 添加右键播放功能 outformat.i ...

  7. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. HTML标签自定义属性(转)

    HTML标签可以自定义属性 HTML标签可以自定义属性,但是我们要考虑其在IE.Firefox以及chrome下的兼容性问题.例如: <div id="newTest" my ...

  9. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  10. python基础——迭代

    python基础——迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for .. ...