c#純粋なコードでデスクトップショートカットを作成し、プログラムメニュー項目を作成し、お気に入りにWebページを追加
7833 ワード
c#純粋なコードでデスクトップショートカットを作成し、プログラムメニュー項目を作成し、お気に入りにWebページを追加
本章ソースSrcダウンロードアドレス:http://download.csdn.net/detail/testcs_dn/5141580
「スタートメニュー」プログラムメニュー項目:
お気に入りに追加:
関連関数コード:
本章ソースSrcダウンロードアドレス:http://download.csdn.net/detail/testcs_dn/5141580
本章ソースSrcダウンロードアドレス:http://download.csdn.net/detail/testcs_dn/5141580
「スタートメニュー」プログラムメニュー項目:
お気に入りに追加:
関連関数コード:
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="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);
}
/// <summary>
///
/// </summary>
/// <param name="url"> </param>
/// <param name="description"> </param>
/// <param name="folderName"> </param>
/// <param name="iconLocation"> </param>
/// <param name="workingDirectory"> </param>
/// <returns></returns>
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);
}
本章ソースSrcダウンロードアドレス:http://download.csdn.net/detail/testcs_dn/5141580