COMのサンプルコードとしてショートカットを作成します
ソースコード
ファイル名:comMakeShotCut.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
ファイル名:Program.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
class Program
{
static void Main()
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string shortcutPath = System.IO.Path.Combine(desktop, "TestShortcut.lnk");
CreateShortcut(shortcutPath,
targetPath: @"C:\Windows\System32\notepad.exe",
arguments: "",
description: "Notepad Shortcut");
Console.WriteLine("Shortcut created: " + shortcutPath);
}
static void CreateShortcut(string shortcutFile, string targetPath, string arguments, string description)
{
// COMオブジェクト(ShellLink)を生成
var shellLink = (IShellLinkW)new CShellLink();
shellLink.SetPath(targetPath);
shellLink.SetArguments(arguments);
shellLink.SetDescription(description);
// .lnk を保存する
var persistFile = (IPersistFile)shellLink;
persistFile.Save(shortcutFile, false);
}
}
// COM クラス (CLSID_ShellLink)
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
class CShellLink
{
}
// IShellLinkW インターフェイス
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
interface IShellLinkW
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath,
out IntPtr pfd, int fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short hotkey);
void SetHotkey(short hotkey);
void GetShowCmd(out int iShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int iIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIconIndex);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
// IPersistFile インターフェイス
[ComImport]
[Guid("0000010B-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPersistFile
{
void GetClassID(out Guid pClassID);
void IsDirty();
void Load([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, int dwMode);
void Save([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, bool fRemember);
void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] out string ppszFileName);
}
実行
実行したところデスクトップにメモ帳のショートカットが作成されました。
Shortcut created: C:\Users\karet\Desktop\TestShortcut.lnk


コメント