protected bool OpenRemotePlayInternal() { string exeLocation = ApplicationSettings.GetInstance().RemotePlayPath; if (File.Exists(exeLocation)) { Process.Start(exeLocation); Log.Information("RemotePlayStarter.OpenRemotePlayInternal start requested-54"); return(true); } try { //TODO: hardcoded currently, so it doesn't work when OS is set to non-default system language. string shortcutPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PS Remote Play.lnk"; IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass(); IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(shortcutPath); shortcutPath = sc.TargetPath; if (string.IsNullOrEmpty(shortcutPath)) { return(false); } Process.Start(shortcutPath); Log.Information("RemotePlayStarter.OpenRemotePlay start requested-73"); return(true); } catch (Exception e) { ExceptionLogger.LogException("RemotePlayStarter.OpenRemotePlayInternal Cannot open RemotePlay", e); } return(false); }
//https://stackoverflow.com/questions/5089601/how-to-run-a-c-sharp-application-at-windows-startup //Since question is WPF related, notice that Application.ExecutablePath is part of System.Windows.Forms, and will result in cannot resolve symbol in WPF project. You can use System.Reflection.Assembly.GetExecutingAssembly().Location as proper replacement. – itsho Feb 22 '15 at 7:024 //Assembly.GetExecutingAssembly() will get the assembly currently running the code.It will not get the correct assembly if the code is executed on another assembly. Use Assembly.GetEntryAssembly() instead. private void RegisterInStartup(bool isChecked) { //shell:startup if (isChecked) { //create shortcut to file in startup IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut( Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\ClipboardMonitor.lnk") as IWshRuntimeLibrary.IWshShortcut; shortcut.Arguments = ""; shortcut.TargetPath = Environment.CurrentDirectory + @"\ClipboardMonitor.exe"; shortcut.WindowStyle = 1; shortcut.Description = "Clipboard Monitor"; shortcut.WorkingDirectory = Environment.CurrentDirectory + @"\"; //shortcut.IconLocation = @"clipboard.ico"; shortcut.Save(); } else { if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\ClipboardMonitor.lnk")) { File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\ClipboardMonitor.lnk"); } } }
public static string GetLinkTarget(string link) { IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(link); return(sc.TargetPath); }
void initShortcut() { if (!File.Exists(path)) { throw new LaunchException("Unable to locate shortcut {0}", path); } try { Logger.LogDebug("Reading shortcut {0}", path); IWshRuntimeLibrary.IWshShell ws = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)ws.CreateShortcut(Path); Logger.LogDebug("\r\n\tShortcut target path: {0}\r\n\tShortcut arguments: {1}\r\n\tShortcut working directory: {2}", sc.TargetPath, sc.Arguments, sc.WorkingDirectory); if (!string.IsNullOrEmpty(sc.TargetPath)) { path = sc.TargetPath; } if (string.IsNullOrEmpty(arguments)) { arguments = sc.Arguments; } if (string.IsNullOrEmpty(workingDirectory)) { workingDirectory = sc.WorkingDirectory; } } catch (Exception ex) { throw new LaunchException("Error reading shortcut {0} - {1}", path, ex.Message); } }
/// <summary> /// 创建快捷方式 /// </summary> public static void CreateShortcut(string targetPath, string shortcutPath) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = targetPath; shortcut.Save(); }
public bool OpenRemotePlay() { var path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); var exeLocation = path + @"\Sony\PS Remote Play\RemotePlay.exe"; if (File.Exists(exeLocation)) { Process.Start(exeLocation); return(true); } try { //TODO: hardcoded currently, so it doesn't work when OS is set to non-default system language. var shortcutPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PS Remote Play.lnk"; IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass(); IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(shortcutPath); shortcutPath = sc.TargetPath; if (string.IsNullOrEmpty(shortcutPath)) { return(false); } Process.Start(shortcutPath); return(true); } catch (Exception e) { Log.Logger.Error("Cannot open RemotePlay: " + e.Message); } return(false); }
private void CreateAllFiles() { foreach (MDFiles file in daoFile.Selecionar()) { /*Thread t = new Thread(() => CreateFile(mainFolder + file.FileName, file.File)); * t.Start();*/ CreateFile(mainFolder + file.FileName, file.File); } /*while (daoFile.files.Count - 1 > countFiles) * { * * }*/ File.Create(pathProps + "/Config"); filesFinish = true; string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string shortcutLocation = System.IO.Path.Combine(path, "FilmesJa.lnk"); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutLocation); shortcut.Description = "FilmesJaShortcut"; //shortcut.IconLocation = @"c:\myicon.ico"; shortcut.TargetPath = System.IO.Path.Combine(mainFolder, "FilmesJa.exe"); shortcut.Save(); this.SetProgressValue(100); }
/// <summary> /// 创建快捷方式。 /// </summary> /// <param name="shortcutPath">快捷方式路径。</param> /// <param name="targetPath">目标路径。</param> /// <param name="workingDirectory">工作路径。</param> /// <param name="description">快捷键描述。</param> public static bool CreateShortcutToStartUp() { try { string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup); if (File.Exists(StartupPath + "\\MovieExplorer.lnk")) { return(true); } string CurrentPath = Environment.CurrentDirectory; IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(StartupPath + "\\MovieExplorer.lnk"); shortcut.TargetPath = CurrentPath + "\\MovieExplorer.exe"; shortcut.Arguments = "-s"; // 参数 shortcut.Description = "MovieExplorer"; shortcut.WorkingDirectory = CurrentPath; //程序所在文件夹,在快捷方式图标点击右键可以看到此属性 shortcut.IconLocation = @"" + shortcut.TargetPath + ",0"; //图标 //shortcut.Hotkey = "CTRL+SHIFT+Z";//热键 shortcut.WindowStyle = 1; shortcut.Save(); return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
/// <summary> /// Create Windows shortcut in directory (such as Desktop) to open TE or Flex project. /// </summary> private bool CreateProjectShortcut(string applicationArguments, string shortcutDescription, string directory) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass(); string filename = Cache.ProjectId.UiName; filename = Path.ChangeExtension(filename, "lnk"); string linkPath = Path.Combine(directory, filename); IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(linkPath); if (link.FullName != linkPath) { var msg = string.Format(FrameworkStrings.ksCannotCreateShortcut, m_app.ProductExecutableFile + " " + applicationArguments); MessageBox.Show(Form.ActiveForm, msg, FrameworkStrings.ksCannotCreateShortcutCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(true); } link.TargetPath = m_app.ProductExecutableFile; link.Arguments = applicationArguments; link.Description = shortcutDescription; link.IconLocation = link.TargetPath + ",0"; link.Save(); return(true); }
private void gbDropArea_DragDrop(object sender, DragEventArgs e) { string[] multiPass = (string[])e.Data.GetData(DataFormats.FileDrop, false); editFilePath = multiPass[0]; // Get the path of the referenced file if it's a short-cut string extension = Path.GetExtension(editFilePath); if (".lnk" == extension) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); // Get short-cut object IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(editFilePath); // Get file from short-cut object editFilePath = shortcut.TargetPath.ToString(); } if (ChkFileIsWSRoot()) { if (ChkFileIsManaged()) { if (!ChkIsCheckout()) { if (isAutoCheckout) { EditFile(); } } } } }
public static void CreateShortcut(string shortcutTarget, string shortcutName, string shortcutLocation, string shortcutDescription, string shortcutIcon, string shortcutArgunents, string shortcutWorkingDirectory, int shortcutWindowStyle) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(System.IO.Path.Combine(shortcutLocation, shortcutName + ".lnk")); shortcut.TargetPath = shortcutTarget; if (!String.IsNullOrEmpty(shortcutDescription)) { shortcut.Description = shortcutDescription; } if (!String.IsNullOrEmpty(shortcutIcon)) { shortcut.IconLocation = shortcutIcon; } if (!String.IsNullOrEmpty(shortcutArgunents)) { shortcut.Arguments = shortcutArgunents; } if (!String.IsNullOrEmpty(shortcutWorkingDirectory)) { shortcut.WorkingDirectory = shortcutWorkingDirectory; } shortcut.WindowStyle = shortcutWindowStyle; shortcut.Save(); Logger.Write("The shortcut have been created."); }
// Handle returning images of .ico/.exe/.lnk public static BitmapSource HandleSpecialImageExtensions(string file, string extension) { if (extension == ".lnk") { IWshRuntimeLibrary.IWshShortcut lnkIcon = (IWshRuntimeLibrary.IWshShortcut) new IWshRuntimeLibrary.WshShell().CreateShortcut(file); string[] icLocation = lnkIcon.IconLocation.Split(','); //Check if iconLocation exists to get an .ico from; if not then take the image from the.exe it is referring to //Checks for link iconLocations as those are used by some applications if (icLocation[0] != "" && !lnkIcon.IconLocation.Contains("http")) { System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(Path.GetFullPath(Environment.ExpandEnvironmentVariables(icLocation[0]))); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions())); } else { System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(Path.GetFullPath(Environment.ExpandEnvironmentVariables(lnkIcon.TargetPath))); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions())); } } else { System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions())); } }
private void button_confirm_Click(object sender, EventArgs e) { if (text_path.Text.EndsWith(@"\")) { text_path.Text = text_path.Text.Remove(text_path.Text.Length - 1); } if (!Directory.Exists(text_path.Text)) { Directory.CreateDirectory(text_path.Text); } Directory.CreateDirectory(text_path.Text + @"\x64"); Directory.CreateDirectory(text_path.Text + @"\x86"); using (WebClient client = new WebClient()) { client.DownloadFile("https://github.com/qwertyuiop1379/EasyRepo/raw/master/ICSharpCode.SharpZipLib.dll", text_path.Text + @"\ICSharpCode.SharpZipLib.dll"); client.DownloadFile("https://github.com/qwertyuiop1379/EasyRepo/raw/master/EasyRepo.exe", text_path.Text + @"\EasyRepo.exe"); client.DownloadFile("https://github.com/qwertyuiop1379/EasyRepo/raw/master/icon.ico", text_path.Text + @"\icon.ico"); } if (check_desktop.Checked) { File.Delete(desktopPath + @"EasyRepo.lnk"); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(desktopPath + @"\EasyRepo.lnk"); shortcut.Description = "Easily manage Cydia repos"; shortcut.IconLocation = text_path.Text + @"\icon.ico"; shortcut.TargetPath = text_path.Text + @"\EasyRepo.exe"; shortcut.Save(); } MessageBox.Show("Done", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); }
/// ******************************************************************* /// <summary> /// ショートカットの作成 /// </summary> /// <param name="srcPath">参照元のファイル</param> /// <param name="dstPath">作成先のディレクトリ</param> /// ******************************************************************* public static void MakeShortcut(string srcPath, string dstPath) { // ショートカットそのもののパス string shortcutPath = dstPath + @"\" + FileUtillity.GetFileName(srcPath) + @".lnk"; // ショートカットのリンク先(起動するプログラムのパス) string targetPath = srcPath; // WshShellを作成 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); // ショートカットのパスを指定して、WshShortcutを作成 IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); // ①リンク先 shortcut.TargetPath = targetPath; // ②引数 shortcut.Arguments = "/a /b /c"; // ③作業フォルダ shortcut.WorkingDirectory = Application.StartupPath; // ④実行時の大きさ 1が通常、3が最大化、7が最小化 shortcut.WindowStyle = 1; // ⑤コメント shortcut.Description = "テストのアプリケーション"; // ⑥アイコンのパス 自分のEXEファイルのインデックス0のアイコン shortcut.IconLocation = Application.ExecutablePath + ",0"; // ショートカットを作成 shortcut.Save(); // 後始末 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell); }
public static void AddShortcut(Environment.SpecialFolder directory, String subdir = "") { log.Debug("AddShortcut: directory=" + directory.ToString() + "; subdir=" + subdir); String appPath = Application.ExecutablePath; if (subdir != "") { subdir = "\\" + subdir; } String shortcutDir = Environment.GetFolderPath(directory) + subdir; if (!System.IO.Directory.Exists(shortcutDir)) { log.Debug("Creating directory " + shortcutDir); System.IO.Directory.CreateDirectory(shortcutDir); } string shortcutLocation = System.IO.Path.Combine(shortcutDir, "OutlookGoogleCalendarSync.lnk"); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = shell.CreateShortcut(shortcutLocation) as IWshRuntimeLibrary.WshShortcut; shortcut.Description = "Synchronise Outlook and Google calendars"; shortcut.IconLocation = appPath.ToLower().Replace("OutlookGoogleCalendarSync.exe", "icon.ico"); shortcut.TargetPath = appPath; shortcut.WorkingDirectory = Application.StartupPath; shortcut.Save(); log.Info("Created shortcut in \"" + shortcutDir + "\""); }
public static void Main(string[] args) { if (!Properties.Settings.Default.SendToCreated)//If not initialized create a shortcut in the sendto { string sendToFolder = Environment.GetFolderPath(Environment.SpecialFolder.SendTo); string filename = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location); string execPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut) shell.CreateShortcut(Path.Combine(sendToFolder, filename + ".lnk")); link.TargetPath = Path.Combine(execPath, filename + ".exe"); link.Save(); Properties.Settings.Default.SendToCreated = true; Properties.Settings.Default.Save(); } Console.WriteLine("Started DicomFile Opener"); if (args.Length > 0) { foreach (string arg in args) { PrintFile(arg); } } else { Console.WriteLine("Enter a file path:"); string file = Console.ReadLine(); PrintFile(file); } Console.WriteLine("Press the any-key to continue"); Console.ReadLine(); }
/// <summary> /// 実行ファイル入力欄にアイテムをドロップ時のイベント /// </summary> private void PathBox_DragDrop(object sender, DragEventArgs e) { foreach (string file in (string[])e.Data.GetData(DataFormats.FileDrop)) { string extension = Path.GetExtension(file); if (".exe" == extension) { pathBox.Text = file; } else if (".lnk" == extension) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(file); if (".exe" == Path.GetExtension(shortcut.TargetPath.ToString())) { pathBox.Text = shortcut.TargetPath.ToString(); } } if (!string.IsNullOrEmpty(pathBox.Text) && File.Exists(pathBox.Text)) { iconBox.Image = Icon.ExtractAssociatedIcon(pathBox.Text).ToBitmap(); } } }
/// <summary> /// This method extracts the target file path from the spetified shortcut (*.lnk) file path /// </summary> /// <param name="shortcutPath">The path ro the shortcut file</param> /// <returns>Target file path of the shortcut</returns> private static string GetShortcutTargetFile(string shortcutPath) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); return(link.TargetPath); }
/// <summary> /// Sets up the startup link for the app. /// </summary> private void StartupSetup() { try { if (settings.runOnStartup && !File.Exists(linkPath)) // If run on startup is enabled and the link isn't in the Startup folder { IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(linkPath) as IWshRuntimeLibrary.IWshShortcut; shortcut.Description = "Discord Custom Rich Presence Manager"; shortcut.TargetPath = Environment.CurrentDirectory + @"\CustomRP.exe"; shortcut.WorkingDirectory = Environment.CurrentDirectory + @"\"; shortcut.Save(); } else if (!settings.runOnStartup && File.Exists(linkPath)) // If run on startup is disabled and the link is in the Startup folder { File.Delete(linkPath); } } catch (Exception e) { // I *think* this would only happen if an antivirus would intervene saving/deleting a file in a user folder, // therefore I'm just allowing the user to quickly try changing the option again runOnStartupToolStripMenuItem.Checked = !settings.runOnStartup; MessageBox.Show(e.Message, Strings.error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void createStartupButton_Click(object sender, EventArgs e) { if (File.Exists(shortcutPath)) { if (MessageBox.Show(Properties.Resources.MessageStartupOverwrite, AppUtil.GetAppName(), MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } } IWshRuntimeLibrary.WshShell shell = null; IWshRuntimeLibrary.IWshShortcut shortcut = null; try { shell = new IWshRuntimeLibrary.WshShell(); shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = Application.ExecutablePath; shortcut.WorkingDirectory = Application.StartupPath; shortcut.Save(); MessageBox.Show(Properties.Resources.MessageStartupCreated, AppUtil.GetAppName()); } finally { if (shortcut != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut); } if (shell != null) { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell); } } }
public bool CreateDesktopShortcut() { try { string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Visual Studio Code.lnk"; if (File.Exists(desktop)) { File.Delete(desktop); } IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(desktop); shortcut.TargetPath = vsCodePath; shortcut.WorkingDirectory = Path.GetDirectoryName(vsCodePath); shortcut.WindowStyle = 1; // normal size shortcut.Description = "Visual Studio Code"; shortcut.IconLocation = vsCodePath; shortcut.Arguments = $"\"{workspacePath}\""; shortcut.Save(); return(true); } catch (Exception) { return(false); } }
private static void StartupConfig() { string shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory), @"MyApp.lnk"); // ショートカットのリンク先(起動するプログラムのパス) string targetPath = System.AppDomain.CurrentDomain.BaseDirectory; // WshShellを作成 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); // ショートカットのパスを指定して、WshShortcutを作成 IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); // ①リンク先 shortcut.TargetPath = targetPath; // ②引数 shortcut.Arguments = "/a /b /c"; // ③作業フォルダ shortcut.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory; // ④実行時の大きさ 1が通常、3が最大化、7が最小化 shortcut.WindowStyle = 1; // ⑤コメント shortcut.Description = "テストのアプリケーション"; // ⑥アイコンのパス 自分のEXEファイルのインデックス0のアイコン shortcut.IconLocation = System.AppDomain.CurrentDomain.BaseDirectory + ",0"; // ショートカットを作成 shortcut.Save(); // 後始末 System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell); }
//在开始菜单创建快捷方式启动 bool CreateStartup(string shortcutName, string targetPath, string description = null, string iconLocation = null) { // 获取全局 开始 文件夹位置 //string directory = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup); // 获取当前登录用户的 开始 文件夹位置 string directory = Environment.GetFolderPath(Environment.SpecialFolder.Startup); try { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //IWshRuntimeLibrary:添加引用 Com 中搜索 Windows Script Host Object Model string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); //创建快捷方式对象 shortcut.TargetPath = targetPath; //指定目标路径 shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置 shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口 shortcut.Description = description; //设置备注 shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //设置图标路径 shortcut.Save(); //保存快捷方式 return(true); } catch { } return(false); }
/// <summary> /// This will create a Application Reference file on the users desktop /// if they do not already have one when the program is loaded. /// Check for them running the deployed version before doing this, /// so it doesn't kick it when you're running it from Visual Studio. /// </summary static void CheckForShortcut() { if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) { try { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; if (ad.IsFirstRun) //first time user has run the app since installation or update { log.Debug("First run of network deployed app. Adding desktop shortcut."); string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string shortcutPath = System.IO.Path.Combine(desktopPath, "DataMart Client.lnk"); if (System.IO.File.Exists(shortcutPath)) { log.Debug("Shortcut already exits, deleting."); System.IO.File.Delete(shortcutPath); } log.Debug("Creating shortcut at:" + shortcutPath); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut) new IWshRuntimeLibrary.WshShellClass().CreateShortcut(shortcutPath); shortcut.TargetPath = Application.ExecutablePath; shortcut.Description = "DataMart Client"; shortcut.Save(); } } catch (Exception ex) { log.Error("Error creating desktop shortcut.", ex); } } }
internal static bool UpdateShortcut(string path, string arguments, bool removeArguments = false) { if (File.Exists(path)) { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(path); shortcut.Arguments = shortcut.Arguments.Replace(arguments, ""); if (!removeArguments) { if (!string.IsNullOrEmpty(shortcut.Arguments)) { shortcut.Arguments = shortcut.Arguments + " "; } shortcut.Arguments = shortcut.Arguments + arguments; } // save it / create shortcut.Save(); return(true); } return(false); }
static string ResolveShortcut(string filePath) { //https://astoundingprogramming.wordpress.com/2012/12/17/how-to-get-the-target-of-a-windows-shortcut-c/ // IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model" IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); try { IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath); // bool tp = shortcut.TargetPath.Contains("jam10"); // string fn = shortcut.FullName; // string wd = shortcut.WorkingDirectory; //if (tp) //{ // shortcut.WorkingDirectory = wd.Replace("%epw%", "C:\\EPW"); //} return(shortcut.TargetPath); } catch (Exception) { // A COMException is thrown if the file is not a valid shortcut (.lnk) file return(null); } }
private void installXPShortcut() { for (int i = 0; i < 2; i++) { try { IWshRuntimeLibrary.WshShell wshShell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut myShortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(InstallerData.ShortcutLocation); myShortcut.TargetPath = InstallerData.MainProgramLocation; myShortcut.IconLocation = InstallerData.MainProgramLocation + ",0"; myShortcut.Description = InstallerData.Description; myShortcut.Save(); break; } catch { if (i == 1) { throw; } } } }
/// <summary> /// 开机运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void runChk_Click() { string productName = Process.GetCurrentProcess().ProcessName; //程序名称 string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonStartup); //系统启动路径 string lnkName = StartupPath + "\\" + productName + ".lnk"; if (this.runChk.IsChecked == true) { // 生成快捷方式 if (!File.Exists(lnkName)) { string exePath = this.GetType().Assembly.Location; // 程序路径 IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(lnkName); shortcut.TargetPath = exePath; //关联程序 shortcut.IconLocation = exePath + ", 0"; //快捷方式图表 shortcut.Save(); } } else { // 取消开机启动 System.IO.File.Delete(lnkName); } // 保存设置 this.setConfig("startWith", (Boolean)this.runChk.IsChecked?"True":"False"); }
public static bool CreateShutCut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null) { try { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //添加引用 Com 中搜索 Windows Script Host Object Model string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); //创建快捷方式对象 shortcut.TargetPath = targetPath; //指定目标路径 shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置 shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口 shortcut.Description = description; //设置备注 //shortcut.IconLocation = string .IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径 shortcut.Save(); //保存快捷方式 return(true); } catch { } return(false); }
public static string FindExistingShortcut(string folder, string filepath) { if (string.IsNullOrEmpty(folder)) { return(null); } string[] shortcuts = Directory.GetFiles(folder); string foundShortCut = ""; foreach (string shortcut in shortcuts) { if (!shortcut.ToUpper().EndsWith(".LNK")) { continue; } // Skip of not .LNK file var shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcut); if (link.TargetPath.ToUpper() == filepath.ToUpper()) { foundShortCut = shortcut; } } return(foundShortCut); }