using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.IO; //private void button1_Click(object sender, EventArgs e)
//{
// Shortcut.CreateShortcut(Shortcut.GetDeskDir() + "\\初学VC记录点滴.lnk", @"http://blog.csdn.net/testcs_dn", Shortcut.GetDeskDir(), "初学VC记录点滴", AppDomain.CurrentDomain.BaseDirectory + "favicon.ico");
// MessageBox.Show("创建成功!");
//} //private void button2_Click(object sender, EventArgs e)
//{
// if (Shortcut.AddFavorites("http://blog.csdn.net/testcs_dn", "初学VC记录点滴", null, AppDomain.CurrentDomain.BaseDirectory + "favicon.ico"))
// {
// MessageBox.Show("添加成功!"); // }
// else
// {
// MessageBox.Show("添加失败");
// }
//} //private void button3_Click(object sender, EventArgs e)
//{
// if (Shortcut.CreateProgramsShortcut("http://blog.csdn.net/testcs_dn", "初学VC记录点滴", "初学VC记录点滴", AppDomain.CurrentDomain.BaseDirectory + "favicon.ico"))
// {
// MessageBox.Show("添加成功!"); // }
// else
// {
// MessageBox.Show("添加失败");
// }
//} public class Shortcut
{
[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 = ; 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 = )] 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 = )] out string pszName,
int cch); [PreserveSig]
void SetDescription(
[MarshalAs(UnmanagedType.LPWStr)] string pszName); [PreserveSig]
void GetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = )] out string pszDir,
int cch); [PreserveSig]
void SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr)] string pszDir); [PreserveSig]
void GetArguments(
[MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = )] 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 = )] 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 static bool CreateShortcut2(string targetPath)
{
string lnkname = Path.GetFileNameWithoutExtension(targetPath);
Shortcut.CreateShortcut(Shortcut.GetDeskDir() +string.Format( @"\{0}.lnk", lnkname), targetPath, Shortcut.GetDeskDir(), lnkname);
return true;
}
public const int SW_SHOWNORMAL = ;
/// <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, );
} 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 = Shortcut.GetDeskDir();
}
return Shortcut.CreateShortcut(Shortcut.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 = Shortcut.GetProgramsDir();
}
string shortcutPath = Shortcut.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 Shortcut.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 = Shortcut.GetProgramsDir();
}
string shortcutPath = Shortcut.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 Shortcut.CreateShortcut(shortcutPath, url, workingDirectory, description, iconLocation);
} internal const uint SHGFP_TYPE_CURRENT = ;
internal const int MAX_PATH = ;
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(, 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# LnkHelper的更多相关文章

随机推荐

  1. java多线程快速入门(二十二)

    线程池的好处: 避免我们过多的去new线程,new是占资源的(GC主要堆内存) 提高效率 避免浪费资源 提高响应速度 作用:会把之前执行某个线程完毕的线程不会释放掉会留到线程池中给下一个调用的线程直接 ...

  2. 在线版区间众数 hzw的代码。。

    /* 查询区间众数,要求强制在线 设有T个块 1.众数只可能在大块[L,R]里或者两端[l,L) (R,r]里出现 2.大块的众数只要预处理打表一下即可,复杂度n*T(这样的区间有T*T个) 3.两端 ...

  3. AI-解析器-request.data内部如何按照自定义解析格式-解析数据

    QUESTION:post方法中调用request.data方法时,当在Courseview类中添加parser_classes=[ForParser,],就可以将数据解析成parser_classe ...

  4. Navicat Premium 12.1.8.0安装与激活

    本文介绍Navicat Premium 12.1.8.0的安装.激活与基本使用. 博主所提供的激活文件理论支持Navicat Premium 12.0.x系列和Navicat Premium 12.1 ...

  5. 关于js渲染网页时爬取数据的思路和全过程(附源码)

    于js渲染网页时爬取数据的思路 首先可以先去用requests库访问url来测试一下能不能拿到数据,如果能拿到那么就是一个普通的网页,如果出现403类的错误代码可以在requests.get()方法里 ...

  6. Cookie中设置了 HttpOnly,Secure 属性,有效的防止XSS攻击,X-Frame-Options 响应头避免点击劫持

    属性介绍: 1) secure属性当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输(ssl),即 只能在 HTTPS 连接中被浏览器传递到服务器端进行会话验证, 如果是 HT ...

  7. 金蝶开k3wise客户端访问中间层不时提示远程服务器不存在或不可用

    此问题一般是发生在WIN7客户端+中间层为WIN2008 R2系统,一般是中间层启用guest引起: a. 从Windows Vista以后, Negotiate (http://msdn.micro ...

  8. .NetCore 下开发独立的(RPL)含有界面的组件包 (二)扩展中间件及服务

    .NetCore 下开发独立的(RPL)含有界面的组件包 (一)准备工作 .NetCore 下开发独立的(RPL)含有界面的组件包 (二)扩展中间件及服 务 .NetCore 下开发独立的(RPL)含 ...

  9. kickstart-G

    感觉自己很蠢,large数据只能交一次,忘记这回事了 A题 O(n^2)解法,用vector<set> 缓存j后面的数据,减少一重循环 #include <string> #i ...

  10. JavaScript动态加载CSS和JS文件

    var dynamicLoading = { css: function(path){ if(!path || path.length === 0){ throw new Error('argumen ...