Fixed saving shortcut icon

This commit is contained in:
Terry MacDonald
2021-11-01 21:48:40 +13:00
parent bd0f3484bb
commit e3d78d3dc1
2 changed files with 24 additions and 33 deletions

View File

@ -26,8 +26,8 @@ using System.Resources;
[assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")]
// Version information // Version information
[assembly: AssemblyVersion("2.1.0.83")] [assembly: AssemblyVersion("2.1.0.89")]
[assembly: AssemblyFileVersion("2.1.0.83")] [assembly: AssemblyFileVersion("2.1.0.89")]
[assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: NeutralResourcesLanguageAttribute( "en" )]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]

View File

@ -1006,31 +1006,22 @@ namespace DisplayMagician
public void ReplaceShortcutIconInCache() public void ReplaceShortcutIconInCache()
{ {
string newShortcutIconFilename; // Figure out if we need to remove the old file
if (_savedShortcutIconCacheFilename != null)
{
// Work out the name of the shortcut we'll save. // Work out the name of the shortcut we'll save.
newShortcutIconFilename = Path.Combine(Program.AppShortcutPath, $"{UUID}.ico"); string oldShortcutIconFilename = _savedShortcutIconCacheFilename;
logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename is {newShortcutIconFilename}."); logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Old shortcut Icon filename is {oldShortcutIconFilename}.");
if (System.IO.File.Exists(oldShortcutIconFilename))
// If the new shortcut icon should be named differently
// then change the name of it
if (!newShortcutIconFilename.Equals(_savedShortcutIconCacheFilename))
{ {
logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename {newShortcutIconFilename} is different to the old shortcut Icon filename {_savedShortcutIconCacheFilename}."); logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Deleting old shortcut Icon filename {oldShortcutIconFilename}.");
if (System.IO.File.Exists(_savedShortcutIconCacheFilename)) System.IO.File.Delete(oldShortcutIconFilename);
{
logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Deleting old shortcut Icon filename {_savedShortcutIconCacheFilename}.");
System.IO.File.Delete(_savedShortcutIconCacheFilename);
} }
logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Creating the new shortcut Icon filename {newShortcutIconFilename} (calling SaveShortcutIconToCache)."); }
// Now we save the new file
SaveShortcutIconToCache(); SaveShortcutIconToCache();
} }
else
{
logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename {newShortcutIconFilename} matches old shortcut Icon filename {_savedShortcutIconCacheFilename} so skipping the rename.");
}
}
public void SaveShortcutIconToCache() public void SaveShortcutIconToCache()
@ -1039,29 +1030,29 @@ namespace DisplayMagician
// Work out the name of the shortcut we'll save. // Work out the name of the shortcut we'll save.
_savedShortcutIconCacheFilename = Path.Combine(Program.AppShortcutPath, $"{UUID}.ico"); _savedShortcutIconCacheFilename = Path.Combine(Program.AppShortcutPath, $"{UUID}.ico");
logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Planning on saving shortcut icon to cache as {_savedShortcutIconCacheFilename}."); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Planning on saving shortcut icon to cache as {_savedShortcutIconCacheFilename}.");
Icon shortcutIcon; MultiIcon shortcutIcon = new MultiIcon();
try try
{ {
logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Creating Icon from Shortcut bitmap."); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Creating Icon from Shortcut bitmap.");
// Get an Hicon for the shortcutBitmap // Create a new
IntPtr Hicon = _shortcutBitmap.GetHicon(); SingleIcon si = shortcutIcon.Add("icon");
// Create a new icon from the handle. si.Add(_shortcutBitmap);
shortcutIcon = Icon.FromHandle(Hicon); shortcutIcon.SelectedIndex = 0;
logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving shortcut icon to cache with {_savedShortcutIconCacheFilename} as the name."); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving shortcut icon to cache with {_savedShortcutIconCacheFilename} as the name.");
using (FileStream fs = new FileStream(_savedShortcutIconCacheFilename, FileMode.Create)) shortcutIcon.Save(_savedShortcutIconCacheFilename, MultiIconFormat.ICO);
shortcutIcon.Save(fs);
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Warn(ex, $"ShortcutItem/SaveShortcutIconToCache: Exception while trying to save the Shortcut icon."); logger.Warn(ex, $"ShortcutItem/SaveShortcutIconToCache: Exception while trying to save the Shortcut icon.");
shortcutIcon.Clear();
// If we fail to create an icon any other way, then we use the default profile icon // If we fail to create an icon any other way, then we use the default profile icon
logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Using the Display Profile icon for {_profileToUse.Name} as the icon instead."); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Using the Display Profile icon for {_profileToUse.Name} as the icon instead.");
shortcutIcon = Properties.Resources.DisplayMagician; SingleIcon si = shortcutIcon.Add("icon2");
si.Add(Properties.Resources.DisplayMagician);
shortcutIcon.SelectedIndex = 0;
logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving the Display Profile icon for {_profileToUse.Name} to {_savedShortcutIconCacheFilename}."); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving the Display Profile icon for {_profileToUse.Name} to {_savedShortcutIconCacheFilename}.");
using (FileStream fs = new FileStream(_savedShortcutIconCacheFilename, FileMode.Create)) shortcutIcon.Save(_savedShortcutIconCacheFilename, MultiIconFormat.ICO);
shortcutIcon.Save(fs);
} }
} }