C#创建快捷方式

http://www.cnblogs.com/zjfree/archive/2011/01/18/1937970.html

 

 

 

C#创建快捷方式

需要先引用COM组件 Interop.IWshRuntimeLibrary.dll 如下图

代码

01private void CreateLnk(string lnkPath)
02{
03    if (!System.IO.File.Exists(lnkPath))
04    {
05        IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
06        IWshRuntimeLibrary.IWshShortcut shortCut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(lnkPath);
07        shortCut.TargetPath = Application.ExecutablePath;
08        shortCut.WindowStyle = 1;
09        shortCut.Description = Application.ProductName + Application.ProductVersion;
10        shortCut.IconLocation = Application.ExecutablePath;
11        shortCut.WorkingDirectory = Application.StartupPath;
12        shortCut.Save();
13    }
14}

获取桌面路径

1string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//" + Application.ProductName + ".lnk";

获取启动文件夹路径

1string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "//" + Application.ProductName + ".lnk";

 

操作注册表实现自启动

操作方法就是给注册表的 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run 添加程序路径

1RegistryKey key = Registry.LocalMachine;
2key = key.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run", true);
3key.SetValue(Application.ProductName, Application.ExecutablePath);

注意此方法在Win7下测试报错!System.UnauthorizedAccessException: 试图执行未经授权的操作。


创建URL快捷方式

 

1string deskDir = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
2using (StreamWriter writer = new StreamWriter(deskDir + "//aaaa.url"))
3{
4    writer.WriteLine("[InternetShortcut]");
5    writer.WriteLine("URL=http://www.163.com/");
6    writer.Flush();
7}

 

示例下载:http://files.cnblogs.com/zjfree/linkTo.rar

运行环境:WIN2003 + VS2005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值