惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
腾讯CDC
博客园_首页
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
SecWiki News
SecWiki News
美团技术团队
P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
D
DataBreaches.Net
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Schneier on Security
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Forbes - Security
Forbes - Security
D
Docker
T
Tenable Blog
S
Secure Thoughts
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

博客园 - 一碗豆芽汤(OneV)

C#一些基础知识回顾 2021年立个Flag,回顾十多年来的IT职业之路-----没事瞎BB 使用 Visual Studio 创建 .NET 5 控制台应用程序 log4net将日志进行分类,保存到不同的目录当中 .net 使用PowerShell获取电脑中的UUID .Net MVC中访问PC网页时,自动切换到移动端对应页面 C# 全角符号转半角 微信开发第二篇:工具篇 微信开发第一篇:问题篇 [转载]ODAC (odp.net) 开发到部署 【项目开发】LigerUI+MVC的应用 VS中两个常用辅助工具 sqlite中常见的问题总结 第1次做面试官的随谈(二) 第1次做面试官的随谈(一) [转载收录]ASP.NET 2.0加密Web.config - 一碗豆芽汤(OneV) - 博客园 关于重构之Switch的处理【二】 - 一碗豆芽汤(OneV) - 博客园 重构的例子 关于重构之Switch的处理【一】如果是有序的话,如何处理
转,有用
一碗豆芽汤(OneV) · 2011-02-26 · via 博客园 - 一碗豆芽汤(OneV)

C#创建快捷方式

2009-09-15 17:19

快捷方式:
[attach]568709[/attach]
快捷方式一般由以下几个部分构成:
1.快捷方式的名字
2.快捷方式所指向的目标所在的位置
3.快捷方式所指向的目标的工作目录
4.激活该快捷方式的热键
5.快捷方式所指向的目标运行时的窗口风格(普通、最大化和最小化)
6.该快捷方式的描述性文字
7.快捷方式的图标所在的位置
引用:
用WSH直接创建快捷方式:
1.首先要添加引用.
添加引用的方法非常简单,右击你的项目并选择添加引用,
选择 COM 选项卡并选择 Windows Script Host Object Model
如图所示:
[attach]568710[/attach]
[attach]568711[/attach]
2.简简单单的界面
[attach]568712[/attach]
3.引用命名空间

复制内容到剪贴板

代码:

//添加的引用
using System.Runtime.InteropServices;//互动服务
using IWshRuntimeLibrary;

4.创建快捷方式(注释中有详细说明)

复制内容到剪贴板

代码:

//实例化WshShell对象
WshShell shell = new WshShell();

//通过该对象的 CreateShortcut 方法来创建 IWshShortcut 接口的实例对象
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//ShortCut.lnk");

//设置快捷方式的目标所在的位置(源程序完整路径)
shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

//应用程序的工作目录
//当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
shortcut.WorkingDirectory = System.Environment.CurrentDirectory;

//目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)
shortcut.WindowStyle = 1;

//快捷方式的描述
shortcut.Description = "ChinaDforce YanMang";

//可以自定义快捷方式图标.(如果不设置,则将默认源文件图标.)
//shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";

//设置应用程序的启动参数(如果应用程序支持的话)
//shortcut.Arguments = "/myword /d4s";

//设置快捷键(如果有必要的话.)
//shortcut.Hotkey = "CTRL+ALT+D";

//保存快捷方式
shortcut.Save();

引用:
特点(优缺点):
我这人最大的缺点就是不喜欢总结"优点",优点略.
缺点:
用这种方法写的程序,必须有Interop.IWshRuntimeLibrary.dll跟着,
才能正确执行.对于创建"单文件程序"的人来讲,麻烦了吧.
如图:
通过创建VBS,并执行,创建方式:1.首先看一下VBS创建快捷方式的代码:

复制内容到剪贴板

代码:

'VBS实例
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop") '获得桌面目录
set oShellLink = WshShell.CreateShortcut(strDesktop & "\D4S.lnk") '快捷方式存放目录及名称
oShellLink.TargetPath = "X:\Program Files\XXX.exe" '指向的可执行文件
oShellLink.WindowStyle = 1 '运行方式(窗体打开的方式)
oShellLink.Hotkey = "CTRL+SHIFT+F" '快捷键
oShellLink.IconLocation = "X:\Program Files\XXX.exe, 0" '图标(同样可不指定)
oShellLink.Description = "ChinaDforce YanMang" '备注信息
oShellLink.WorkingDirectory = "X:\Program Files\" '起始目录
oShellLink.Save '保存快捷方式

2.那我们如何在C#中使用VBS呢?
方法我想应该有很多吧!
在这里介绍一种"最笨"但最直接的方法.
思路如下:

复制内容到剪贴板

代码:

>>> 生成VBS全部代码文本;
>>> 写入临时文件"temp.vbs";
>>> 用Process打开这个文件执行.

3.下面是C#中实现的关键代码:

复制内容到剪贴板

代码:

//生成VBS代码
string vbs = this.CreateVBS();
//以文件形式写入临时文件夹
this.WriteToTemp(vbs);
//调用Process执行
this.RunProcess();

复制内容到剪贴板

代码:

///
/// 创建VBS代码
///
///
private string CreateVBS()
{
string vbs = string.Empty;

vbs += ("set WshShell = WScript.CreateObject(\"WScript.Shell\")\r\n");
vbs += ("strDesktop = WshShell.SpecialFolders(\"Desktop\")\r\n");
vbs += ("set oShellLink = WshShell.CreateShortcut(strDesktop & \"\\D4S.lnk\")\r\n");
vbs += ("oShellLink.TargetPath = \"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"\r\n");
vbs += ("oShellLink.WindowStyle = 1\r\n");
vbs += ("oShellLink.Description = \"ChinaDforce YanMang\"\r\n");
vbs += ("oShellLink.WorkingDirectory = \"" + System.Environment.CurrentDirectory + "\"\r\n");
vbs += ("oShellLink.Save");

return vbs;
}

复制内容到剪贴板

代码:

///
/// 写入临时文件
///
///
private void WriteToTemp(string vbs)
{
if (!string.IsNullOrEmpty(vbs))
{
       //临时文件
       string tempFile = Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "\\temp.vbs";
       //写入文件
       FileStream fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
       try
       {
         //这里必须用UnicodeEncoding. 因为用UTF-8或ASCII会造成VBS乱码
         System.Text.UnicodeEncoding uni = new UnicodeEncoding();
         byte[] b = uni.GetBytes(vbs);
         fs.Write(b, 0, b.Length);
         fs.Flush();
         fs.Close();
       }
       catch (Exception ex)
       {
         MessageBox.Show(ex.Message, "写入临时文件时出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
       finally
       {
         //释放资源
         fs.Dispose();
       }
}
}

复制内容到剪贴板

代码:

///
/// 执行VBS中的代码
///
private void RunProcess()
{
string tempFile = Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "\\temp.vbs";
if (File.Exists(tempFile))
{
       //执行VBS
       Process.Start(tempFile);
}
}

当然不要忘了清理临时文件!哈哈

复制内容到剪贴板

代码:

private void btn退出_Click(object sender, EventArgs e)
{
Application.Exit();
//清除临时文件
File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Templates) + "\\temp.vbs");
}

引用:
强调一点:
在写入VBS文件时,一定要用UnicodeEncoding.
因为UTF-8和ASCII码,都会导致VBS生成快捷方式的时候,
产生乱码,而导致快捷方式错误.
本人原来使用UTF8Encoding的时候,不放在包含中文的路径中还可以,但一出现中文就挂了!
困扰我好半天,才发现的这个细节.