1. 程式人生 > >C#製作檔案快捷方式

C#製作檔案快捷方式

1.生成檔案快捷方式

//IWshRuntimeLibrary是com元件
//需要引用 Script   Host   Object   Model 元件

IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
            IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut("c://a.lnk");
         

            shortcut.TargetPath = @"c:/a.txt";//檔案實體地址
            shortcut.Arguments = "";//引數
            shortcut.Description = "附件快捷方式";
            shortcut.Hotkey = "CTRL+SHIFT+N";
            shortcut.IconLocation = "notepad.exe, 0";
            shortcut.FullName = "開啟163";
            shortcut.Save();

2.生成ie快捷方式

IWshRuntimeLibrary.IWshShell_Class wshShell = new IWshRuntimeLibrary.IWshShell_ClassClass();
            IWshRuntimeLibrary.IWshURLShortcut shortcut = wshShell.CreateShortcut(@"c:/a.url") as IWshRuntimeLibrary.IWshURLShortcut;
            shortcut.TargetPath = "http://www.csdn.net/";

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

生成ie快捷方式2

string SaveLnkDirectory = System.IO.Path.GetDirectoryName(filePath) + "//";
            string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
            string fileLnkFullName = fileName + ".url";
            string SaveLnkPath = SaveLnkDirectory + fileLnkFullName;
            if (File.Exists(SaveLnkPath))
                SaveLnkPath = SaveLnkDirectory + fileName+"1.url";

            System.IO.StreamWriter objWriter = System.IO.File.CreateText(SaveLnkPath);
            objWriter.WriteLine("[InternetShortcut]");
            objWriter.WriteLine("URL=http://www.csdn.net/");
            objWriter.Close();