http://www.cnblogs.com/zjfree/archive/2011/01/18/1937970.html
C#创建快捷方式
需要先引用COM组件 Interop.IWshRuntimeLibrary.dll 如下图
代码
01 | private void CreateLnk( string lnkPath) |
03 | if (!System.IO.File.Exists(lnkPath)) |
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; |
获取桌面路径
1 | string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//" + Application.ProductName + ".lnk" ; |
获取启动文件夹路径
1 | string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "//" + Application.ProductName + ".lnk" ; |
操作注册表实现自启动
操作方法就是给注册表的 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run 添加程序路径
1 | RegistryKey key = Registry.LocalMachine; |
2 | key = key.OpenSubKey( @"SOFTWARE/Microsoft/Windows/CurrentVersion/Run" , true ); |
3 | key.SetValue(Application.ProductName, Application.ExecutablePath); |
注意此方法在Win7下测试报错!System.UnauthorizedAccessException: 试图执行未经授权的操作。
创建URL快捷方式
1 | string deskDir = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
2 | using (StreamWriter writer = new StreamWriter(deskDir + "//aaaa.url" )) |
4 | writer.WriteLine( "[InternetShortcut]" ); |
示例下载:http://files.cnblogs.com/zjfree/linkTo.rar
运行环境:WIN2003 + VS2005