shortcut saving fix

This commit is contained in:
seraphima
2024-06-19 00:06:57 +02:00
parent ba13f5c0c4
commit a62d95c8fa
3 changed files with 41 additions and 11 deletions

View File

@@ -61,6 +61,15 @@
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<COMReference Include="Shell32">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<Content Include="images\DefaultIcon.ico">

View File

@@ -84,7 +84,12 @@ namespace ProjectsEditor.Utils
public static void SaveIcon(Bitmap icon, string path)
{
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate);
if (Path.Exists(path))
{
File.Delete(path);
}
FileStream fileStream = new FileStream(path, FileMode.CreateNew);
using (var memoryStream = new MemoryStream())
{
icon.Save(memoryStream, ImageFormat.Png);

View File

@@ -159,20 +159,36 @@ namespace ProjectsEditor.ViewModels
private void CreateShortcut(Project project)
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string shortcutAddress = FolderUtils.Desktop() + $"\\{project.Name}.lnk";
string shortcutIconFilename = FolderUtils.Temp() + $"\\{project.Name}.ico";
string shortcutAddress = Path.Combine(FolderUtils.Desktop(), project.Name + ".lnk");
string shortcutIconFilename = Path.Combine(FolderUtils.Temp(), project.Id + ".ico");
Bitmap icon = ProjectIcon.DrawIcon(ProjectIcon.IconTextFromProjectName(project.Name));
ProjectIcon.SaveIcon(icon, shortcutIconFilename);
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = $"Project Launcher {project.Id}";
shortcut.TargetPath = Path.Combine(basePath, "PowerToys.ProjectsLauncher.exe");
shortcut.Arguments = project.Id;
shortcut.WorkingDirectory = basePath;
shortcut.IconLocation = shortcutIconFilename;
shortcut.Save();
try
{
// Workaround to be able to create a shortcut with unicode filename
File.WriteAllBytes(shortcutAddress, Array.Empty<byte>());
// Create a ShellLinkObject that references the .lnk file
Shell32.Shell shl = new Shell32.Shell();
Shell32.Folder dir = shl.NameSpace(FolderUtils.Desktop());
Shell32.FolderItem itm = dir.Items().Item($"{project.Name}.lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// Set the .lnk file properties
lnk.Description = $"Project Launcher {project.Id}";
lnk.Path = Path.Combine(basePath, "PowerToys.ProjectsLauncher.exe");
lnk.Arguments = project.Id.ToString();
lnk.WorkingDirectory = basePath;
lnk.SetIconLocation(shortcutIconFilename, 0);
lnk.Save(shortcutAddress);
}
catch (Exception ex)
{
Logger.LogError($"Shortcut creation error: {ex.Message}");
}
}
public void SaveProjectName(Project project)