使用C#创建,查看,修改,删除快捷方式
使用C#创建,查看,修改,删除快捷方式
- 引用COM组件
1)右键“引用”,“添加引用”。
2) 选择“COM组件”,找到“Windows Script Host Object Model”,然后确定。 - 创建快捷方式
/// <summary>
/// 为某个文件创建快捷方式(例如为xxx.exe创建快捷方式)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCreate_Click(object sender, EventArgs e)
{
//创建快捷方式前,先获取要创建快捷方式的文件信息(例如xxx.exe的文件信息)
string workingDirectory = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\"; //获取该文件(如xxx.exe)的所处文件夹路径
string targetPath = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\QuickHider.exe"; //获取该文件(如xxx.exe)的所处路径
//创建快捷方式的步骤
string directory = @"C:\Users\AY_Format\Desktop\"; //创建的快捷方式后,该快捷方式所处的文件夹的路径(根据情况自定路径)
string shortcutName = "QuickHider快捷方式.lnk"; //要创建的快捷方式名称(.lnk为快捷方式后缀)
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(directory + shortcutName); //创建快捷方式对象
shortcut.TargetPath = targetPath; //指定目标路径 (右键快捷方式的目标(T))
shortcut.WorkingDirectory = workingDirectory; //设置起始位置 (右键快捷方式的起始位置(S))
shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口 (右键快捷方式的运行方式(R))
shortcut.Description = "这是用代码创建的快捷方式";//设置备注,鼠标放在图标上提示改文字 (右键快捷方式的备注(O))
//shortcut.IconLocation = ""; //设置图标路径,这里不设置 (快捷方式的图标)
shortcut.Save(); //保存快捷方式
}
- 查看快捷方式
/// <summary>
/// 读取快捷方式信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRead_Click(object sender, EventArgs e)
{
string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //需要读取的快捷方式路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource); //获取快捷方式对象
string targetPath = shortcut.TargetPath; //读取右键快捷方式的目标信息
string workingDirectory = shortcut.WorkingDirectory; //读取右键快捷方式的起始位置信息
int windowStyle = shortcut.WindowStyle; //读取右键快捷方式的运行方式信息
string description = shortcut.Description; //读取右键快捷方式的备注信息
string iconLocation = shortcut.IconLocation; //读取快捷方式的图标信息
lblInfo.Text = "目标信息:" + targetPath + "\n起始位置信息:" + workingDirectory + "\n运行方式信息:" + windowStyle + "\n备注信息:" + description + "\n图标信息:" + iconLocation;
}
- 修改快捷方式
/// <summary>
/// 修改快捷方式属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdate_Click(object sender, EventArgs e)
{
string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //获取要更改的快捷方式路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource); //获取快捷方式对象
shortcut.Description = "这是修改后的备注信息!"; //修改备注,鼠标放在图标上提示改文字 (右键快捷方式的备注(O))
//其他属性也是如此
shortcut.Save();
}
- 删除快捷方式
/// <summary>
/// 删除快捷方式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"))
{
System.IO.File.Delete(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk");
MessageBox.Show("删除成功!");
}
}
完整代码
using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Shortcut
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
/// <summary>
/// 为某个文件创建快捷方式(例如为xxx.exe创建快捷方式)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCreate_Click(object sender, EventArgs e)
{
//创建快捷方式前,先获取要创建快捷方式的文件信息(例如xxx.exe的文件信息)
string workingDirectory = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\"; //获取该文件(如xxx.exe)的所处文件夹路径
string targetPath = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\QuickHider.exe"; //获取该文件(如xxx.exe)的所处路径
//创建快捷方式的步骤
string directory = @"C:\Users\AY_Format\Desktop\"; //创建的快捷方式后,该快捷方式所处的文件夹的路径(根据情况自定路径)
string shortcutName = "QuickHider快捷方式.lnk"; //要创建的快捷方式名称(.lnk为快捷方式后缀)
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(directory + shortcutName); //创建快捷方式对象
shortcut.TargetPath = targetPath; //指定目标路径 (右键快捷方式的目标(T))
shortcut.WorkingDirectory = workingDirectory; //设置起始位置 (右键快捷方式的起始位置(S))
shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口 (右键快捷方式的运行方式(R))
shortcut.Description = "这是用代码创建的快捷方式";//设置备注,鼠标放在图标上提示改文字 (右键快捷方式的备注(O))
//shortcut.IconLocation = ""; //设置图标路径,这里不设置 (快捷方式的图标)
shortcut.Save(); //保存快捷方式
}
/// <summary>
/// 读取快捷方式信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRead_Click(object sender, EventArgs e)
{
string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //需要读取的快捷方式路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource); //获取快捷方式对象
string targetPath = shortcut.TargetPath; //读取右键快捷方式的目标信息
string workingDirectory = shortcut.WorkingDirectory; //读取右键快捷方式的起始位置信息
int windowStyle = shortcut.WindowStyle; //读取右键快捷方式的运行方式信息
string description = shortcut.Description; //读取右键快捷方式的备注信息
string iconLocation = shortcut.IconLocation; //读取快捷方式的图标信息
lblInfo.Text = "目标信息:" + targetPath + "\n起始位置信息:" + workingDirectory + "\n运行方式信息:" + windowStyle + "\n备注信息:" + description + "\n图标信息:" + iconLocation;
}
/// <summary>
/// 修改快捷方式属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdate_Click(object sender, EventArgs e)
{
string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //获取要更改的快捷方式路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource); //获取快捷方式对象
shortcut.Description = "这是修改后的备注信息!"; //修改备注,鼠标放在图标上提示改文字 (右键快捷方式的备注(O))
//其他属性也是如此
shortcut.Save();
}
/// <summary>
/// 删除快捷方式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"))
{
System.IO.File.Delete(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk");
MessageBox.Show("删除成功!");
}
}
}
}
源码下载(窗体+代码):使用C#创建,查看,修改,删除快捷方式