Using WshShell instead of my Shortcut interface, added Shortcut to Start Menu

This commit is contained in:
HorizonCode 2021-05-27 11:33:45 +02:00
parent 0937fa43a3
commit ff43c77b2e
3 changed files with 46 additions and 56 deletions

View File

@ -96,7 +96,6 @@
<Compile Include="Utils\ProcessUtil.cs" />
<Compile Include="Utils\RegistryUtil.cs" />
<Compile Include="Utils\ReleaseStreamUtil.cs" />
<Compile Include="Objects\Shortcut.cs" />
<Compile Include="Objects\UpdateState.cs" />
<Compile Include="Utils\OsuUtil.cs" />
<Compile Include="MainWindow.xaml.cs">
@ -138,8 +137,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="DK.WshRuntime">
<Version>4.1.3.201115164</Version>
<PackageReference Include="DK.Standard">
<Version>4.137.4070.201115155</Version>
</PackageReference>
<PackageReference Include="ModernWpfUI">
<Version>0.9.4</Version>
@ -151,6 +150,16 @@
<ItemGroup>
<Content Include=".gitignore" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -13,12 +13,13 @@ using ModernWpf.Controls;
using System.Windows.Forms;
using System.Media;
using System.Runtime.InteropServices.ComTypes;
using Shortcut;
using Path = System.Windows.Shapes.Path;
using File = System.IO.File;
using EZPPClient_Installer.Utils;
using Microsoft.Win32;
using System.Windows.Controls;
using System.Threading;
using IWshRuntimeLibrary;
#if DEBUG
using MessageBox = System.Windows.MessageBox;
@ -419,18 +420,30 @@ namespace EZPPClient_Installer
if (type.Equals("Install"))
{
IShellLink link = (IShellLink)new ShellLink();
// setup shortcut information
link.SetDescription("Starts the EZPPClient");
link.SetPath(EZPPFolder + @"\EZPPClient.exe");
// save it
IPersistFile file = (IPersistFile)link;
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string LinkFile = System.IO.Path.Combine(desktopPath, "Start EZPPClient.lnk");
Console.Out.WriteLine("Saving Shortcut to " + LinkFile);
file.Save(LinkFile, false);
string desktopLinkFile = System.IO.Path.Combine(desktopPath, "Start EZPPClient.lnk");
string startMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string startMenuPathPrograms = System.IO.Path.Combine(startMenuPath, "Programs");
string startMenuLinkFile = System.IO.Path.Combine(startMenuPathPrograms, "Start EZPPClient.lnk");
if (!File.Exists(desktopLinkFile))
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(desktopLinkFile);
shortcut.Description = $"Starts the EZPPClient";
shortcut.TargetPath = System.IO.Path.Combine(EZPPFolder, "EZPPClient.exe");
shortcut.Save();
}
if (!File.Exists(startMenuLinkFile))
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(startMenuLinkFile);
shortcut.Description = $"Starts the EZPPClient";
shortcut.TargetPath = System.IO.Path.Combine(EZPPFolder, "EZPPClient.exe");
shortcut.Save();
}
}
InstallButton.Visibility = prevInstallBtnVis;
@ -586,12 +599,19 @@ namespace EZPPClient_Installer
string osuEZPPPath = folderTextbox.Text + @"\EZPPClient";
if (Directory.Exists(osuEZPPPath))
Directory.Delete(osuEZPPPath, true);
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string linkFile = System.IO.Path.Combine(desktopPath, "Start EZPPClient.lnk");
if (File.Exists(linkFile))
File.Delete(linkFile);
string startMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string startMenuPathPrograms = System.IO.Path.Combine(startMenuPath, "Programs");
string startMenuLinkFile = System.IO.Path.Combine(startMenuPathPrograms, "Start EZPPClient.lnk");
if (File.Exists(startMenuLinkFile))
File.Delete(startMenuLinkFile);
_ = new ContentDialog()
{
Title = "Success!",

View File

@ -1,39 +0,0 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace Shortcut
{
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
internal interface IShellLink
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
}