準備
- 参照設定を追加します。
– Windows Script Host Object Model
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
サンプルコード (C#)
using IWshRuntimeLibrary;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
IWshShell_Class wsc = new IWshShell_Class();
WshShortcut ws;
string sDeskPath = Environment.GetFolderPath(
Environment.SpecialFolder.System);
string sSysPath = Environment.GetFolderPath(
Environment.SpecialFolder.DesktopDirectory);
ws = (WshShortcut)wsc.CreateShortcut(sSysPath + @"\ノートパッド.lnk");
ws.TargetPath = sDeskPath + @"\notepad.exe";
ws.IconLocation = "notepad.exe, 0";
ws.Description = "ノートパッドへのショートカット";
ws.WorkingDirectory = sDeskPath;
ws.Save();
}
}
}
解説
ノートパッドのショートカットをデスクトップに作成しています。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.06.07 Visual Studio 2015 Professional (C# 6.0)
