C#创建快捷方式
需要先引用COM组件 Interop.IWshRuntimeLibrary.dll 如下图
代码
private void CreateLnk(string lnkPath) { if (!System.IO.File.Exists(lnkPath)) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortCut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(lnkPath); shortCut.TargetPath = Application.ExecutablePath; shortCut.WindowStyle = 1; shortCut.Description = Application.ProductName + Application.ProductVersion; shortCut.IconLocation = Application.ExecutablePath; shortCut.WorkingDirectory = Application.StartupPath; shortCut.Save(); } }
获取桌面路径
string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + Application.ProductName + ".lnk";
获取启动文件夹路径
string lnkPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".lnk";
操作注册表实现自启动
操作方法就是给注册表的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 添加程序路径
RegistryKey key = Registry.LocalMachine; key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); key.SetValue(Application.ProductName, Application.ExecutablePath);
注意此方法在Win7下测试报错!System.UnauthorizedAccessException: 试图执行未经授权的操作。
创建URL快捷方式
string deskDir = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); using (StreamWriter writer = new StreamWriter(deskDir + "\\aaaa.url")) { writer.WriteLine("[InternetShortcut]"); writer.WriteLine("URL=http://www.163.com/"); writer.Flush(); }
示例下载:https://files.cnblogs.com/zjfree/linkTo.rar
运行环境:WIN2003 + VS2005
欢迎转载,转载请注明:转载自[ http://www.cnblogs.com/zjfree/ ]