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. [UOJ#34]多项式乘法

    [UOJ#34]多项式乘法 试题描述 这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入 第一行两个整数 n 和 m,分别表示两个多项式的次数. 第二行 n+1 个整数,分别表示第一个多 ...

  2. ZeroMQ(java)中对IO的封装(StreamEngine)

    哎,各种各样杂七杂八的事情...好久没有看代码了,其实要搞明白一个与IO相关的框架,最好的办法就是把它的I/0的读写两个过程搞清楚...例如在netty中,如果能将eventLoop的运行原理搞清楚, ...

  3. JSP基本面试的试题

    JSP基本面试的试题 1.jsp有哪些内置对象作用分别是什么 答:JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应):      request 用户端请求,此请求会包含来自GET/PO ...

  4. 最长公共子序列 NYOJ37

    http://acm.nyist.net/JudgeOnline/problem.php?pid=37 先逆转原来的字符串,再用原来的字符串跟逆转后的字符串进行比较,求得的最长公共子序列就是回文串,也 ...

  5. 试试markdown

    看看有没有wrapper... list first list second list third list fourth list in list 1 list list list > < ...

  6. MyBatis调用存储过程

    MySQL存储过程 DROP PROCEDURE IF EXISTS transferMoney; -- 实现转账功能的存储过程 CREATE PROCEDURE transferMoney ( IN ...

  7. mysql in 子查询 效率慢 优化(转)

    mysql in 子查询 效率慢 优化(转) 现在的CMS系统.博客系统.BBS等都喜欢使用标签tag作交叉链接,因此我也尝鲜用了下.但用了后发现我想查询某个tag的文章列表时速度很慢,达到5秒之久! ...

  8. i686和x86_64的区别

    找回TCL隐藏分区(转载) 用Wubi安装 Ubuntu 出现(Initranfs)问题的解决方案 i686和x86_64的区别 2009-04-11 08:19:31|  分类: 电脑问题 |  标 ...

  9. Sharepoint 2010 创建栏 计算栏

    SharePoint 创建栏时,可以添加计算字段, 网上查了查,相关资料如下: http://wenku.baidu.com/view/936239e9b8f67c1cfad6b88f.html ht ...

  10. 在windows下用cygwin和eclipse搭建cocos2dx(2.1.4)的android开发环

    一.准备工作 需要下载和安装以下内容,请根据自己的操作系统选择x86和x64(我的是64位win7,我就拿64位说事) 1.jdk-7u25-windows-x64.exe(下载完后直接安装,一直下一 ...