diff options
| author | a1xd <[email protected]> | 2021-09-13 03:39:58 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-09-23 22:34:51 -0400 |
| commit | 6b3eacb1870480be23aaa25b306da316ac96e6d4 (patch) | |
| tree | dde1d995444930bf02646b6440ea153bab0a9afb /grapher | |
| parent | rename driver_settings (diff) | |
| download | rawaccel-6b3eacb1870480be23aaa25b306da316ac96e6d4.tar.xz rawaccel-6b3eacb1870480be23aaa25b306da316ac96e6d4.zip | |
add fn for making a shortcut in startup folder
Diffstat (limited to 'grapher')
| -rw-r--r-- | grapher/Form1.cs | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/grapher/Form1.cs b/grapher/Form1.cs index b41afc2..32c5c77 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -15,6 +15,7 @@ using grapher.Models.Serialized; using grapher.Models; using System.Reflection; using System.Diagnostics; +using System.IO; namespace grapher { @@ -274,5 +275,67 @@ namespace grapher } #endregion Method - } + + static void MakeStartupShortcut(bool gui) + { + var startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + + if (string.IsNullOrEmpty(startupFolder)) + { + throw new Exception("Startup folder does not exist"); + } + + //Windows Script Host Shell Object + Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); + dynamic shell = Activator.CreateInstance(t); + + try + { + // Delete any other RA related startup shortcuts + var candidates = new[] { "rawaccel", "raw accel", "writer" }; + + foreach (string path in Directory.EnumerateFiles(startupFolder, "*.lnk") + .Where(f => candidates.Any(f.Substring(startupFolder.Length).ToLower().Contains))) + { + var link = shell.CreateShortcut(path); + try + { + string targetPath = link.TargetPath; + + if (!(targetPath is null) && + (targetPath.EndsWith("rawaccel.exe") || + targetPath.EndsWith("writer.exe") && + new FileInfo(targetPath).Directory.GetFiles("rawaccel.exe").Any())) + { + File.Delete(path); + } + } + finally + { + Marshal.FinalReleaseComObject(link); + } + } + + var name = gui ? "rawaccel" : "writer"; + + var lnk = shell.CreateShortcut($@"{startupFolder}\{name}.lnk"); + + try + { + if (!gui) lnk.Arguments = Constants.DefaultSettingsFileName; + lnk.TargetPath = $@"{Application.StartupPath}\{name}.exe"; + lnk.Save(); + } + finally + { + Marshal.FinalReleaseComObject(lnk); + } + + } + finally + { + Marshal.FinalReleaseComObject(shell); + } + } + } } |