C#如何利用(Windows Script Host)COM组件来创建快捷方式

102
补充展位
Pages_Weblog_Get#d1bb0210-0cb5-4b90-96f3-f94d9c98f569
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

C#如何利用 Windows Script Host Object Mode COM组件来实现快捷方式的创建生成

不多说废话 开整

1.首先引用COM组件 “Windows Script Host Object Model”

2.再引用命名空间 IWshRuntimeLibrary

3.将这个下面这个类拷贝进项目(已做好封装 相应调用方法即可)

public class ShortcutManager { 
WshShell shell = new WshShell();        
string ShortFileName;      
public ShortcutManager(string ShortFileName) {      
		this.ShortFileName = ShortFileName;       
}      
/// <summary>       
/// 创建快捷方式     
/// </summary>       
/// <param name="ShortFileName">快捷方式文件名称 无需后缀名</param>      
/// <param name="ShortFileDirectory">快捷方式文件存储位置</param>        
public void Create(string ShortName, string ShortFileDirectory)  {     
	if (!System.IO.Directory.Exists(ShortFileDirectory))  {          
	System.IO.Directory.CreateDirectory(ShortFileDirectory);           
	 }            
	string shortcutPath = Path.Combine(ShortFileDirectory, string.Format("{0}.lnk", ShortName));  
	if (System.IO.File.Exists(shortcutPath))            {       
		return;       
	}           
	IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象           
	shortcut.TargetPath = ShortFileName;//指定目标路径          
	shortcut.WorkingDirectory = Path.GetDirectoryName(ShortFileName);//设置起始位置           
	shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口     
	shortcut.IconLocation = ShortFileName;           
	shortcut.Save();//保存快捷方式    
}

调用例子 存储到桌面的快捷方式

string exe = this.GetType().Assembly.Location;//当前程序的完整路径
ShortcutManager manager = new ShortcutManager(exe);
string desktop=System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
manager.Create("我的快捷方式", desktop);
补充展位 Pages_Weblog_Get#0
补充展位 Pages_Weblog_Get#1
补充展位 Pages_Weblog_Get#2
专题推荐
暂无内容
补充展位 Pages_Weblog_Get#3