private void CreateShortcutOnDesktop() { //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary; String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Tale.lnk"); if (!System.IO.File.Exists(shortcutPath)) { // 获取当前应用程序目录地址 String exePath = Process.GetCurrentProcess().MainModule.FileName; IWshShell shell = new WshShell(); // 确定是否已经创建的快捷键被改名了 foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk")) { WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item); if (tempShortcut.TargetPath == exePath) { return; } } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); //MessageBox.Show(exePath, "exePath"); shortcut.TargetPath = exePath; shortcut.Arguments = ""; // 参数 shortcut.Description = "Tale Tools Dev:codin QQ:2420498526 && biezhi QQ:921293209"; shortcut.WorkingDirectory = Environment.CurrentDirectory; //程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = exePath; //图标,该图标是应用程序的资源文件 //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下 shortcut.WindowStyle = 1; shortcut.Save(); } }
/// <summary> /// スタートアップ登録 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CheckBox_Checked(object sender, RoutedEventArgs e) { string shortcutPath = getShortcutPath(); string targetPath = Assembly.GetExecutingAssembly().Location; WshShell shell = new WshShell(); WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = targetPath; //アイコンのパス shortcut.IconLocation = targetPath + ",0"; // 引数 //shortcut.Arguments = "/a /b /c"; // 作業フォルダ shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); // 実行時のウィンドウの大きさ 1:通常 3:最大化 7:最小化 shortcut.WindowStyle = 1; // コメント shortcut.Description = "そら時計スタートアップ登録"; shortcut.Save(); //後始末 Marshal.FinalReleaseComObject(shortcut); Marshal.FinalReleaseComObject(shell); }
private void CreateShortcutOnDesktop() { //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary; String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "NFC_ID.lnk"); if (!System.IO.File.Exists(shortcutPath)) { // 获取当前应用程序目录地址 String exePath = Process.GetCurrentProcess().MainModule.FileName; IWshShell shell = new WshShell(); // 确定是否已经创建的快捷键被改名了 foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk")) { WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item); if (tempShortcut.TargetPath == exePath) { return; } } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = exePath; shortcut.Arguments = ""; // 参数 shortcut.Description = "应用程序说明--金坤科创NFC设备7字节uid读取"; shortcut.WorkingDirectory = Environment.CurrentDirectory; //程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = exePath; //图标,该图标是应用程序的资源文件 shortcut.WindowStyle = 1; shortcut.Save(); } }
public static void CreateShortcutOnDesktop() { //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary; string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "ChobitsLive Minecraft.lnk"); if (!System.IO.File.Exists(shortcutPath)) { // 获取当前应用程序目录地址 string exePath = Process.GetCurrentProcess().MainModule.FileName; IWshShell shell = new WshShell(); // 确定是否已经创建的快捷键被改名了 foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk")) { WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item); if (tempShortcut.TargetPath == exePath) { return; } } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = exePath; shortcut.Arguments = ""; // 参数 shortcut.Description = "ChobitsLive社团MC服务器客户端"; shortcut.WorkingDirectory = Environment.CurrentDirectory; //程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = exePath; //图标,该图标是应用程序的资源文件 //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下 shortcut.WindowStyle = 1; shortcut.Save(); } }
private void AddNewItem() { NewItem newItem = new NewItem(); this.InsertItem(newItem, 0); newItem.AddNewItem += (sender, e) => { using (NewLnkFileDialog dlg = new NewLnkFileDialog()) { dlg.FileFilter = $"{AppString.Dialog.Program}|*.exe;*.bat;*.cmd;*.vbs;*.vbe;*.js;*.jse;*.wsf"; if (dlg.ShowDialog() != DialogResult.OK) { return; } string lnkPath = $@"{SendToPath}\{ObjectPath.RemoveIllegalChars(dlg.ItemText)}.lnk"; lnkPath = ObjectPath.GetNewPathWithIndex(lnkPath, ObjectPath.PathType.File); using (WshShortcut shortcut = new WshShortcut(lnkPath)) { shortcut.TargetPath = dlg.ItemFilePath; shortcut.WorkingDirectory = Path.GetDirectoryName(dlg.ItemFilePath); shortcut.Arguments = dlg.Arguments; shortcut.Save(); } DesktopIni.SetLocalizedFileNames(lnkPath, dlg.ItemText); this.InsertItem(new SendToItem(lnkPath), 2); } }; }
/// <summary> /// 创建桌面快捷方式 /// </summary> private void CreateShortcutOnDesktop() { String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Piano.lnk"); if (System.IO.File.Exists(shortcutPath)) { System.IO.File.Delete(shortcutPath); } // 获取当前应用程序目录地址 String exePath = textBox1.Text + "/Piano/Pinao.exe"; IWshShell shell = new WshShell(); // 确定是否已经创建的快捷键被改名了 foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk")) { WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item); if (tempShortcut.TargetPath == exePath) { return; } } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = exePath; shortcut.Arguments = ""; // 参数 shortcut.Description = "CACode's For Piano"; shortcut.WorkingDirectory = Environment.CurrentDirectory; //程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = exePath; //图标,该图标是应用程序的资源文件 //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下 shortcut.WindowStyle = 1; shortcut.Save(); }
} //Exit Option private void CreateShortcut(string shortcutPathName, bool create) { if (create) { try { string shortcutTarget = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, appname + ".exe"); WshShell myShell = new WshShell(); WshShortcut myShortcut = (WshShortcut)myShell.CreateShortcut(shortcutPathName); myShortcut.TargetPath = shortcutTarget; //The exe file this shortcut executes when double clicked myShortcut.IconLocation = shortcutTarget + ",0"; //Sets the icon of the shortcut to the exe`s icon myShortcut.WorkingDirectory = System.Windows.Forms.Application.StartupPath; //The working directory for the exe myShortcut.Arguments = ""; //The arguments used when executing the exe myShortcut.Save(); //Creates the shortcut } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } else { try { if (System.IO.File.Exists(shortcutPathName)) { System.IO.File.Delete(shortcutPathName); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } }
/// <summary> /// 在桌面创建快捷方式 /// </summary> /// <param name="ShortcutName">快捷方式名称</param> /// <param name="TargetPath">快捷方式目标路径</param> /// <returns>true:创建成功, false:创建失败,已存在</returns> public static bool CreateShortcutOnDesktop(string ShortcutName, string TargetPath) { //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary; String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), ShortcutName + ".lnk"); //有相同快捷方式,不创建 if (System.IO.File.Exists(shortcutPath)) { return(false); } //没有相同快捷方式,创建 else { IWshShell shell = new WshShell(); WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); //目标 shortcut.TargetPath = TargetPath; //目标后面跟的参数,会有个空格隔开 //shortcut.Arguments = ""; //备注 //shortcut.Description = ""; //起始位置 //shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性 //shortcut.WorkingDirectory = ""; //图标,该图标是应用程序的资源文件 //shortcut.IconLocation = exePath; //快捷键 //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下 shortcut.WindowStyle = 1; shortcut.Save(); return(true); } }
/// <summary> /// 创建快捷方式 /// </summary> /// <param name="path">快捷方式路径</param> /// <param name="targetPath">目标路径</param> public static void CreateShortcut(string path, string targetPath) { WshShell shell = new WshShellClass(); WshShortcut shortcut = shell.CreateShortcut(path) as WshShortcut; shortcut.TargetPath = targetPath; shortcut.Save(); }
/// <summary> /// Create Shortcut /// </summary> /// <param name="shortcutDir">Source directory where the shortcut will be saved.</param> /// <param name="targetPath">Target directory where the source for the shortcut is located.</param> public static void CreateShortcut(string shortcutDir, string targetPath) { string path = shortcutDir + ".lnk"; WshShellClass shell = new WshShellClass(); WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(path); shortcut.TargetPath = targetPath; shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); shortcut.Arguments = "Argument"; shortcut.Description = "This is a Shortcut"; shortcut.Save(); }
public static Task CreateShortCut() { WshShell ScriptingShell = new WshShell(); WshShortcut ShortcutShell = (WshShortcut)ScriptingShell.CreateShortcut(ShortcutLocation); ShortcutShell.TargetPath = ShortcutTarget; ShortcutShell.IconLocation = ShortcutTarget + ",0"; ShortcutShell.Description = "Document Repository"; ShortcutShell.WorkingDirectory = AppDirectory; ShortcutShell.Arguments = ""; ShortcutShell.Save(); return(Task.CompletedTask); }
public void MakeLauncher(string command, string args, string launcherPath) { string shortcutFilePath = Path.ChangeExtension( launcherPath, ShortcutExtension); WshShell wshShell = new IWshRuntimeLibrary.WshShell(); WshShortcut shortcut = (WshShortcut)wshShell .CreateShortcut(shortcutFilePath); shortcut.TargetPath = command; shortcut.Arguments = args; shortcut.Save(); return; }
private void Create_shortcut() { if (System.IO.File.Exists(Get_shortcut_path()) ? MessageBox.Show("The shortcut \"" + Shortcut_name_TextBox.Text + "\" already exists. Are you sure you want to overwrite it?", "Shortcut already exists", MessageBoxButtons.YesNo) == DialogResult.Yes : true) { WshShell shell = new WshShell(); WshShortcut shortcut = shell.CreateShortcut(Get_shortcut_path()); shortcut.TargetPath = "C:\\Program Files" + (System.IO.Directory.Exists("C:\\Program Files (x86)") ? " (x86)" : "") + @"\Google\Chrome\Application\chrome.exe"; shortcut.Arguments = "--profile-directory=Default --app=\"" + Get_website_address() + "\""; shortcut.Save(); MessageBox.Show("Shortcut created"); } }
public static void CreateShortcut(SpecialFolders specialFolder, string name, string exePath) { WshShell ws = new WshShellClass(); string path = GetShortcutPath(ws, specialFolder, name); WshShortcut shortcut = ws.CreateShortcut(path) as WshShortcut; shortcut.TargetPath = exePath; shortcut.WindowStyle = 1; //shortcut.Hotkey = "CTRL+SHIFT+F"; shortcut.IconLocation = exePath + ", 0"; shortcut.Description = name; shortcut.WorkingDirectory = Path.GetDirectoryName(exePath); shortcut.Save(); }
private void AddNewItem() { FilePath = $@"{SendToList.SendToPath}\{ObjectPath.RemoveIllegalChars(ItemText)}.lnk"; FilePath = ObjectPath.GetNewPathWithIndex(FilePath, ObjectPath.PathType.File); WshShortcut shortcut = new WshShortcut { FullName = FilePath, TargetPath = Command, WorkingDirectory = Path.GetDirectoryName(Command), Arguments = Arguments }; shortcut.Save(); SendToList.DesktopIniWriter.SetValue("LocalizedFileNames", Path.GetFileName(FilePath), ItemText); }
/// <summary> /// Creates or deletes shortcut link to startup OST. /// </summary> /// <param name="create"></param> /// <returns></returns> private bool SetAutoStartup(bool create) { if (create) { try { string appname = Assembly.GetExecutingAssembly().FullName.Remove(Assembly.GetExecutingAssembly().FullName.IndexOf(",")); string shortcutTarget = System.IO.Path.Combine(Application.StartupPath, appname + ".exe"); WshShell myShell = new WshShell(); WshShortcut myShortcut = (WshShortcut)myShell.CreateShortcut(shortcutFileName); myShortcut.TargetPath = shortcutTarget; // Shortcut to OverloadServerTool.exe. myShortcut.IconLocation = shortcutTarget + ",0"; // Use default application icon. myShortcut.WorkingDirectory = Application.StartupPath; // Working directory. myShortcut.Arguments = "-launched"; // Parameters sent to OverloadServerTool.exe. myShortcut.Save(); // Create shortcut. return(true); } catch (Exception ex) { MessageBox.Show($"Unable to create autostart shortcut: {ex.Message}"); } return(false); } else { try { if (System.IO.File.Exists(shortcutFileName)) { System.IO.File.Delete(shortcutFileName); } else { return(false); } } catch (Exception ex) { MessageBox.Show($"Unable to remove autostart shortcut: {ex.Message}"); } return(true); } }
public static string Extract(string ico, string shortcutLocation) { String path = Environment.GetFolderPath(Environment.SpecialFolder.System); Console.WriteLine(path); // string shortcutLocation = @"C:\Users\Gordan\Zune.lnk"; string newIconLocation = path + @"\shell32.dll," + ico; IWshShell_Class Shell = new IWshShell_Class(); // IWshRuntimeLibrary.IWshShell_Class WshShortcut cl = (WshShortcut)Shell.CreateShortcut(shortcutLocation); cl.IconLocation = newIconLocation; cl.Save(); return(""); }
public bool ChangeCommand(WshShortcut shortcut) { using (CommandDialog dlg = new CommandDialog()) { dlg.Command = shortcut.TargetPath; dlg.Arguments = shortcut.Arguments; if (dlg.ShowDialog() != DialogResult.OK) { return(false); } shortcut.TargetPath = dlg.Command; shortcut.Arguments = dlg.Arguments; shortcut.Save(); return(true); } }
private void createShortcut(string shortcutTarget, bool create) { if (create) { try { string linkLocation = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), AppData.ShortCurtDisplay + ".lnk"); string exeLocation = Path.Combine(AppData.ClickOnceLocation, AppData.CurrentFolder); exeLocation = Path.Combine(System.Windows.Forms.Application.StartupPath, EXENAME); if (System.IO.File.Exists(linkLocation)) { System.IO.File.Delete(linkLocation); } WshShell myShell = new WshShell(); WshShortcut myShortcut = (WshShortcut)myShell.CreateShortcut(linkLocation); myShortcut.TargetPath = exeLocation; //The exe file this shortcut executes when double clicked myShortcut.IconLocation = exeLocation + ",0"; //Sets the icon of the shortcut to the exe`s icon myShortcut.WorkingDirectory = System.Windows.Forms.Application.StartupPath; //The working directory for the exe myShortcut.Description = "Click Once Backup"; myShortcut.Arguments = ""; //The arguments used when executing the exe myShortcut.Save(); //Creates the shortcut } catch (Exception ex) { } } else { try { string linkLocation = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), AppData.ShortCurtDisplay + ".lnk"); if (System.IO.File.Exists(linkLocation)) { System.IO.File.Delete(linkLocation); } } catch (Exception ex) { } } }
/// <summary> /// Creates a shortcut file to the specified target file. /// </summary> /// <param name="linkFilePath">Full path where the link is to be created. If a directory is given, the lnk file will be created with the executing assembly's name.</param> /// <param name="targetFilePath">Full path to the file the shortcut is to refer to.</param> /// <param name="targetWorkDirPath">Full path to the working directory of the file the shortcut is to refer to.</param> public static void createShortcut(string linkFilePath, string targetFilePath, string targetWorkDirPath) { // if linkFilePath is a directory, use the name of the target file for the shortcut DirectoryInfo di = new DirectoryInfo(linkFilePath); if (di.Exists) { linkFilePath += extractFilenameWithoutExtension(targetFilePath) + ".lnk"; } // create the shortcut WshShell shell = new WshShell(); WshShortcut link = (WshShortcut)shell.CreateShortcut(linkFilePath); link.TargetPath = targetFilePath; link.WorkingDirectory = targetWorkDirPath; link.Save(); }
private void SetAutoStart() { try { string currentApp = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HueHue" + ".exe"); WshShell shell = new WshShell(); WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\HueHue.lnk"); shortcut.TargetPath = currentApp; shortcut.IconLocation = currentApp + ",0"; shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; shortcut.Arguments = "autostart"; shortcut.Save(); } catch (Exception e) { throw new Exception("Error setting to auto start" + Environment.NewLine + e.Message); } }
public Task CreateDownloaderShortCut() { string AppDataLocal = Path.GetFullPath(ApplicationFolder); string ShortcutTarget = Path.GetFullPath(AppDataLocal + @"\RepositoryDownloader\RepositoryDownloader.exe"); string ShortcutLink = Path.GetFullPath(AppDataLocal + @"\RepositoryDownloader\RepositoryDownloader.lnk"); string ShortcutLocation = Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Repository Downloader.lnk"); string AppDirectory = Path.GetFullPath(AppDataLocal); WshShell ScriptingShell = new WshShell(); WshShortcut ShortcutShell = (WshShortcut)ScriptingShell.CreateShortcut(ShortcutLocation); ShortcutShell.TargetPath = ShortcutTarget; ShortcutShell.IconLocation = ShortcutTarget + ",0"; ShortcutShell.Description = "Repository Downloader"; ShortcutShell.WorkingDirectory = AppDirectory; ShortcutShell.Arguments = ""; ShortcutShell.Save(); return(Task.CompletedTask); }
/// <summary> /// Creates the shortcut at the specified path. /// </summary> /// <param name="binPath">The path to binary.</param> /// <param name="destination">The path to shortcut.</param> /// <param name="arguments">Application arguments.</param> /// <param name="runAsAdmin">if set to <c>true</c> if application need to [run as admin].</param> private void CreateShortcut_(string binPath, string destination, string arguments, bool runAsAdmin) //(string shortcutPathName, bool create) { try { string shortcutTarget = binPath;//System.IO.Path.Combine(Application.StartupPath, appname + ".exe"); WshShell myShell = new WshShell(); WshShortcut myShortcut = (WshShortcut)myShell.CreateShortcut(destination); myShortcut.TargetPath = shortcutTarget; //The exe file this shortcut executes when double clicked myShortcut.IconLocation = shortcutTarget + ",0"; //Sets the icon of the shortcut to the exe`s icon myShortcut.WorkingDirectory = Application.StartupPath; //The working directory for the exe myShortcut.Arguments = ""; //The arguments used when executing the exe myShortcut.Save(); //Creates the shortcut } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 创建桌面快捷方式 /// 借鉴了某位老哥的代码地址:https://blog.csdn.net/luwq168/article/details/78969446 /// </summary> public static void CreateShortcut() { var shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "猛汉王存档备份工具.lnk"); if (!File.Exists(shortcutPath)) { var exePath = Process.GetCurrentProcess().MainModule.FileName; IWshShell shell = new WshShell(); if (CheckShortcutExist()) { return; } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = exePath; shortcut.Arguments = "";// 参数 shortcut.Description = "狩猎之余别忘了备份哦!"; shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; shortcut.IconLocation = exePath; shortcut.WindowStyle = 1; shortcut.Save(); } }
static void Main(string[] args) { WebClient webdl = new WebClient(); if (!System.IO.File.Exists(Path.GetTempPath() + "/ricardo_ico.ico")) { webdl.DownloadFile(ICO_LNK, Path.GetTempPath() + "/ricardo_ico.ico"); } Console.WriteLine("Finished downloading Ricardo icon."); if (!System.IO.File.Exists(Path.GetTempPath() + "/ricardo_bg.png")) { webdl.DownloadFile(BG_LNK, Path.GetTempPath() + "/ricardo_bg.png"); } Console.WriteLine("Finished downloading Ricardo background."); if (!System.IO.File.Exists(Path.GetTempPath() + "/ricardo_u.wav")) { webdl.DownloadFile(U_LNK, Path.GetTempPath() + "/ricardo_u.wav"); } Console.WriteLine("Finished downloading U Got That."); Console.WriteLine("Reiconning links at desktop."); WshShell Shell = new WshShell(); foreach (string lnk in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.lnk", SearchOption.AllDirectories)) { WshShortcut cl = (WshShortcut)Shell.CreateShortcut(lnk); cl.IconLocation = Path.GetTempPath() + "/ricardo_ico.ico"; cl.Save(); Console.WriteLine(ConsoleColor.Green + lnk); } Console.WriteLine("Enough with lnks! Let's set wallpaper."); Wallpaper.GetBackgroud(); Wallpaper.SetBackgroud(Path.GetTempPath() + "/ricardo_bg.png"); SoundPlayer mw = new SoundPlayer(Path.GetTempPath() + "/ricardo_u.wav"); NotepadHelper.ShowMessage("ur pc was danced till the death\nwe are now vibing at ur pc haha\nu got that", "ricard0overtaker::_overtaken"); Console.WriteLine("Finished. Showed the notepad message."); mw.PlaySync(); }
private void AddNewItem() { FilePath = $@"{SendToList.SendToPath}\{ObjectPath.RemoveIllegalChars(ItemText)}.lnk"; FilePath = ObjectPath.GetNewPathWithIndex(FilePath, ObjectPath.PathType.File); WshShortcut shortcut = new WshShortcut { FullName = FilePath }; if (rdoFile.Checked) { ItemCommand = Environment.ExpandEnvironmentVariables(ItemCommand); shortcut.TargetPath = ObjectPath.ExtractFilePath(ItemCommand, out string shortPath); string str = ItemCommand.Substring(ItemCommand.IndexOf(shortPath) + shortPath.Length); shortcut.Arguments = str.Substring(str.IndexOf(" ") + 1); shortcut.WorkingDirectory = Path.GetDirectoryName(shortcut.TargetPath); } else { shortcut.TargetPath = ItemCommand; } shortcut.Save(); DesktopIniHelper.SetLocalizedFileName(FilePath, ItemText); }
/// <summary> /// Creates or removes a shortcut for this application at the specified pathname. /// </summary> /// <param name="shortcutPathName"> /// The path where the shortcut is to be created or removed from including the (.lnk) extension. /// </param> /// <param name="create"> True to create a shortcut or False to remove the shortcut.</param> private void CreateShortcut(string shortcutPathName, bool create) { /// Source https://code.msdn.microsoft.com/windowsdesktop/Create-a-shortcut-for-your-ad3d9cb3 if (create) { try { string shortcutTarget = System.IO.Path.Combine(Application.StartupPath, appname + ".exe"); WshShell myShell = new WshShell(); WshShortcut myShortcut = (WshShortcut)myShell.CreateShortcut(shortcutPathName); myShortcut.TargetPath = shortcutTarget; //The exe file this shortcut executes when double clicked myShortcut.IconLocation = shortcutTarget + ",0"; //Sets the icon of the shortcut to the exe`s icon myShortcut.WorkingDirectory = Application.StartupPath; //The working directory for the exe myShortcut.Arguments = ""; //The arguments used when executing the exe myShortcut.Save(); //Creates the shortcut } catch { throw; } } else { try { if (System.IO.File.Exists(shortcutPathName)) { System.IO.File.Delete(shortcutPathName); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
//窗口显示时事件 private void install_Shown(object sender, EventArgs e) { //开始安装 //创建相关文件和文件夹 //创建安装目录 if (!Directory.Exists(InstallationPath)) { Directory.CreateDirectory(InstallationPath); for (int i = 0; i < 6; i++) { Delay(50); ucProcessLine1.Value = i; } // 5 } //创建配置文件 for (int i = 5; i < 11; i++) { Delay(50); ucProcessLine1.Value = i; } // 10 string config = @"<?xml version=""1.0"" encoding=""utf-8"" ?><configuration><appSettings><add key = ""var"" value = """ + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + @"""></add></appSettings></configuration> "; FileStream fs = new FileStream(InstallationPath + "/andlua.config", FileMode.Create); byte[] data = System.Text.Encoding.Default.GetBytes(config); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); //创建lua文件库目录 if (!Directory.Exists(InstallationPath + "/lua")) { Directory.CreateDirectory(InstallationPath + "/lua"); for (int i = 10; i < 16; i++) { Delay(50); ucProcessLine1.Value = i; } // 15 } //创建dll文件库目录 if (!Directory.Exists(InstallationPath + "/libs")) { Directory.CreateDirectory(InstallationPath + "/libs"); for (int i = 15; i < 21; i++) { Delay(50); ucProcessLine1.Value = i; } // 20 } //创建工程文件目录 if (!Directory.Exists(InstallationPath + "/project")) { Directory.CreateDirectory(InstallationPath + "/project"); for (int i = 20; i < 26; i++) { Delay(50); ucProcessLine1.Value = i; } // 25 } //解压Dll资源包 ucProcessLine1.Value = 26; deszip("libs", InstallationPath + "/libs.zip"); if (Compress(InstallationPath + "/libs", InstallationPath + "/libs.zip", null) != "Success") { MessageBox.Show("安装错误!", "提示"); } for (int i = 26; i < 41; i++) { Delay(50); ucProcessLine1.Value = i; } // 40 //解压Lua资源包 deszip("lua", InstallationPath + "/lua.zip"); if (Compress(InstallationPath + "/lua", InstallationPath + "/lua.zip", null) != "Success") { MessageBox.Show("安装错误!", "提示"); } for (int i = 40; i < 51; i++) { Delay(50); ucProcessLine1.Value = i; } // 50 //安装程序 ucProcessLine1.Value = 51; //创建执行文件 System.IO.File.Copy(System.Windows.Forms.Application.ExecutablePath, InstallationPath + "/AndLua+.exe", true); for (int i = 51; i < 61; i++) { Delay(50); ucProcessLine1.Value = i; } // 60 //创建配置文件 FileStream fs1 = new FileStream(InstallationPath + "/AndLua+.exe.config", FileMode.Create); byte[] data1 = System.Text.Encoding.Default.GetBytes(Properties.Resources.AndLuaConfig); fs1.Write(data1, 0, data1.Length); fs1.Flush(); fs1.Close(); //修改安装路径 string config2 = System.IO.File.ReadAllText(InstallationPath + "/AndLua+.exe.config", Encoding.Default); config2 = config2.Replace(@"C:/Program Files (x86)/AndLuaPlus", InstallationPath); FileStream fs3 = new FileStream(InstallationPath + "/AndLua+.exe.config", FileMode.Create); byte[] data3 = System.Text.Encoding.Default.GetBytes(config2); fs3.Write(data3, 0, data3.Length); fs3.Flush(); fs3.Close(); //创建快捷方式 String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "AndLuaPlus.lnk"); if (!System.IO.File.Exists(shortcutPath)) { String exePath = InstallationPath + "/AndLua+.exe"; IWshShell shell = new WshShell(); foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk")) { WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item); if (tempShortcut.TargetPath == exePath) { return; } } WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = exePath; shortcut.Arguments = ""; shortcut.Description = "Developer Alone"; shortcut.WorkingDirectory = InstallationPath; shortcut.IconLocation = exePath; shortcut.WindowStyle = 1; shortcut.Save(); } for (int i = 60; i < 76; i++) { Delay(50); ucProcessLine1.Value = i; } // 75 //释放资源 ucProcessLine1.Value = 76; DeleteFile(InstallationPath + "/libs.zip"); DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "/libs1.zip"); DeleteFile(AppDomain.CurrentDomain.BaseDirectory + @"/HZH_Controls.dll"); for (int i = 76; i < 86; i++) { Delay(50); ucProcessLine1.Value = i; } // 85 DeleteFile(InstallationPath + "/lua.zip"); for (int i = 85; i < 100; i++) { Delay(50); ucProcessLine1.Value = i; } // 99 //安装完成 Delay(1000); ucProcessLine1.Value = 100; }
// LNK (file shortcuts), URL (Internet shortcuts), PIF (DOS shortcuts) // How to Show File Extensions of Shortcuts (LNK, URL, PIF) in Windows Explorer? // https://www.askvg.com/tip-how-to-show-file-extensions-of-shortcuts-lnk-url-pif-in-windows-explorer/ public static void GreateShortcutLnk(string shortcutLnkFileName, string targetPath) { // Use it like this: // string shortcutLnkTargetFilePath = @"C:\Users\USERNANE\Desktop\ttt.txt"; // string shortcutLnkFileName = Test.CreateGuid(); // Test.GreateShortcutLnk(shortcutLnkFileName, shortcutLnkTargetFilePath); // So the issues is that the COM interface requires admin access, which your end users may not have. If you know they have access then it's not an issue. // However, in cases where your users do not have admin access, the WSH with Dyanmics approach below works. // https://stackoverflow.com/questions/2934420/why-do-i-get-e-accessdenied-when-reading-public-shortcuts-through-shell32 // https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator/2818776#2818776 // Implementation of IWshRuntimeLibrary to create the shortcut // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/xsy6k3ys%28v=vs.84%29 // https://bytescout.com/blog/create-shortcuts-in-c-and-vbnet.html // https://stackoverflow.com/questions/4897655/create-a-shortcut-on-desktop // https://books.google.al/books?id=gchk6fz65WYC&pg=PA518&lpg=PA518&dq=iwshshortcut+msdn&source=bl&ots=WQcHHwNbaj&sig=ACfU3U195Tyu-VL1RozOOClatYnfJAmFcQ&hl=en&sa=X&ved=2ahUKEwixw8Llh83gAhVJEVAKHeKHC68Q6AEwBHoECAIQAQ#v=onepage&q=iwshshortcut%20msdn&f=false // http://www.wengkien.com/2009/01/08/reading-and-creating-shortcuts-information/ string shortcutsPath = Helper.GetShortcutsPath(); string shortcutLnkFilePath = Path.Combine(shortcutsPath, shortcutLnkFileName + Constants.EXT_LNK); if (System.IO.File.Exists(shortcutLnkFilePath)) { // The shortcut is already created. // No need to create the shortcut. return; } // WshShell Object // Provides access to the native Windows shell. // You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. // The WshShell object provides the Environment collection. This collection allows you to handle environmental variables (such as WINDIR, PATH, or PROMPT). // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/aew9yb99%28v%3dvs.84%29 WshShell wshShell = new WshShell(); // CreateShortcut Method // Creates a new shortcut, or opens an existing shortcut. // The CreateShortcut method returns either a WshShortcut object or a WshURLShortcut object. Simply calling the CreateShortcut method does not result in the creation of a shortcut. // The shortcut object and changes you may have made to it are stored in memory until you save it to disk with the Save method. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/xsy6k3ys%28v=vs.84%29 // WshShortcut Object // Allows you to create a shortcut programmatically. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/xk6kst2k%28v%3dvs.84%29 // IWshShortcut iWshShortcut = (IWshShortcut)wshShell.CreateShortcut(shortcutLnkFilePath); WshShortcut wshShortcut = (WshShortcut)wshShell.CreateShortcut(shortcutLnkFilePath); // Arguments Property (WScript Object) // Returns the WshArguments object (a collection of arguments). // The arguments used when executing the exe. The Arguments property contains the WshArguments object (a collection of arguments). // Use a zero-based index to retrieve individual arguments from this collection. // Example: // When Target is "c:\windows\notepad.exe" then Arguments could be "c:\windows\text.txt". So it is telling to open text.txt with notepad. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/z2b05k8s%28v%3dvs.84%29 // wshShortcut.Arguments = ""; // string arguments = wshShortcut.Arguments; // Description Property (Windows Script Host) // Returns a shortcut's description. // The Description property contains a string value describing a shortcut. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/ybdhh477%28v%3dvs.84%29 // wshShortcut.Description = ""; // string description = wshShortcut.Description; // FullName Property (WshShortcut Object) // Returns the fully qualified path of the shortcut object's target. // The FullName property contains a read-only string value indicating the fully qualified path to the shortcut's target. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/7c7x465d%28v%3dvs.84%29 // string fullName = wshShortcut.FullName // Hotkey Property // Assigns a key-combination to a shortcut, or identifies the key-combination assigned to a shortcut. // Example: // "Ctrl+Alt+e"; // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/3zb1shc6%28v%3dvs.84%29 // wshShortcut.Hotkey = ""; // string hotkey = wshShortcut.Hotkey; // IconLocation Property // Assigns an icon to a shortcut, or identifies the icon assigned to a shortcut. // Example: // "notepad.exe, 0"; // Zero is the index // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/3s9bx7at%28v%3dvs.84%29 // wshShortcut.IconLocation = ""; // string iconLocation = wshShortcut.IconLocation; // RelativePath Property // Assigns a relative path to a shortcut, or identifies the relative path of a shortcut. // Example: // If in CreateShortcut only the link name is specified like "linkname.lnk" then we need the RelativePath to be specified like "C:\" // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/85hy4580%28v%3dvs.84%29 // wshShortcut.RelativePath = ""; // TargetPath Property // The path to the shortcut's executable. // This property is for the shortcut's target path only. Any arguments to the shortcut must be placed in the Argument's property. wshShortcut.TargetPath = targetPath; // string targetPath = wshShortcut.TargetPath; // WindowStyle Property // Assigns a window style to a shortcut, or identifies the type of window style used by a shortcut. // The WindowStyle property returns an integer. // The available settings for intWindowStyle: // 1 Activates and displays a window.If the window is minimized or maximized, the system restores it to its original size and position. // 3 Activates the window and displays it as a maximized window. // 7 Minimizes the window and activates the next top-level window. wshShortcut.WindowStyle = 1; // int windowStyle = wshShortcut.WindowStyle; // WorkingDirectory Property (Windows Script Host) // Assign a working directory to a shortcut, or identifies the working directory used by a shortcut. // Is the directory where the shortcut is placed. If you miss it then unexpected result might hapen. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/ae0a4aee%28v%3dvs.84%29 wshShortcut.WorkingDirectory = shortcutsPath; // string WorkingDirectory = wshShortcut.WorkingDirectory; // Save Method (Windows Script Host) // Saves a shortcut object to disk. // After using the CreateShortcut method to create a shortcut object and set the shortcut object's properties, the Save method must be used to save the shortcut object to disk. // The Save method uses the information in the shortcut object's FullName property to determine where to save the shortcut object on a disk. // You can only create shortcuts to system objects. This includes files, directories, and drives (but does not include printer links or scheduled tasks). // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/k5x59zft%28v%3dvs.84%29 wshShortcut.Save(); }