mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
shortcut saving fix
This commit is contained in:
@@ -61,6 +61,15 @@
|
|||||||
<Isolated>false</Isolated>
|
<Isolated>false</Isolated>
|
||||||
<EmbedInteropTypes>true</EmbedInteropTypes>
|
<EmbedInteropTypes>true</EmbedInteropTypes>
|
||||||
</COMReference>
|
</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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="images\DefaultIcon.ico">
|
<Content Include="images\DefaultIcon.ico">
|
||||||
|
|||||||
@@ -84,7 +84,12 @@ namespace ProjectsEditor.Utils
|
|||||||
|
|
||||||
public static void SaveIcon(Bitmap icon, string path)
|
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())
|
using (var memoryStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
icon.Save(memoryStream, ImageFormat.Png);
|
icon.Save(memoryStream, ImageFormat.Png);
|
||||||
|
|||||||
@@ -159,20 +159,36 @@ namespace ProjectsEditor.ViewModels
|
|||||||
private void CreateShortcut(Project project)
|
private void CreateShortcut(Project project)
|
||||||
{
|
{
|
||||||
string basePath = AppDomain.CurrentDomain.BaseDirectory;
|
string basePath = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
string shortcutAddress = FolderUtils.Desktop() + $"\\{project.Name}.lnk";
|
string shortcutAddress = Path.Combine(FolderUtils.Desktop(), project.Name + ".lnk");
|
||||||
string shortcutIconFilename = FolderUtils.Temp() + $"\\{project.Name}.ico";
|
string shortcutIconFilename = Path.Combine(FolderUtils.Temp(), project.Id + ".ico");
|
||||||
|
|
||||||
Bitmap icon = ProjectIcon.DrawIcon(ProjectIcon.IconTextFromProjectName(project.Name));
|
Bitmap icon = ProjectIcon.DrawIcon(ProjectIcon.IconTextFromProjectName(project.Name));
|
||||||
ProjectIcon.SaveIcon(icon, shortcutIconFilename);
|
ProjectIcon.SaveIcon(icon, shortcutIconFilename);
|
||||||
|
|
||||||
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
|
try
|
||||||
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
|
{
|
||||||
shortcut.Description = $"Project Launcher {project.Id}";
|
// Workaround to be able to create a shortcut with unicode filename
|
||||||
shortcut.TargetPath = Path.Combine(basePath, "PowerToys.ProjectsLauncher.exe");
|
File.WriteAllBytes(shortcutAddress, Array.Empty<byte>());
|
||||||
shortcut.Arguments = project.Id;
|
|
||||||
shortcut.WorkingDirectory = basePath;
|
// Create a ShellLinkObject that references the .lnk file
|
||||||
shortcut.IconLocation = shortcutIconFilename;
|
Shell32.Shell shl = new Shell32.Shell();
|
||||||
shortcut.Save();
|
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)
|
public void SaveProjectName(Project project)
|
||||||
|
|||||||
Reference in New Issue
Block a user