Updated Shortcut saving to work with the library

Updated the 'Save To Desktop' button in the
ShortcutLibraryForm to use the CreateShortcut
function in the ShortcutItem, and fixed that so
it now uses the IWshRuntimeLibrary to create
a Windows Script Host that will create the
shortcut.
This commit is contained in:
terrymacdonald 2020-07-21 19:50:41 +12:00
parent 85963b3417
commit c1ce153c68
13 changed files with 1942 additions and 102 deletions

View File

@ -25,7 +25,7 @@ using System.Resources;
namespace HeliosPlus.Shared
{
public class ProfileRepository
public static class ProfileRepository
{
#region Class Variables
// Common items to the class
@ -44,23 +44,12 @@ namespace HeliosPlus.Shared
#endregion
#region Instance Variables
// Individual items per class instance
#endregion
#region Class Constructors
public ProfileRepository()
static ProfileRepository()
{
// Load the Profiles from storage
LoadProfiles();
}
public ProfileRepository(ProfileItem Profile) : this()
{
if (Profile is ProfileItem)
AddProfile(Profile);
}
#endregion
#region Class Properties

View File

@ -243,6 +243,17 @@
<Name>HeliosPlus.Shared</Name>
</ProjectReference>
</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" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -314,11 +314,11 @@ namespace HeliosPlus.Resources {
}
/// <summary>
/// Looks up a localized string similar to Executing &apos;{0}&apos; with &apos;{1}&apos; profile..
/// Looks up a localized string similar to Running &apos;{0}&apos; with &apos;{1}&apos; profile..
/// </summary>
internal static string Executing_application_with_profile {
internal static string Execute_application_with_profile {
get {
return ResourceManager.GetString("Executing_application_with_profile", resourceCulture);
return ResourceManager.GetString("Execute_application_with_profile", resourceCulture);
}
}
@ -629,7 +629,7 @@ namespace HeliosPlus.Resources {
}
/// <summary>
/// Looks up a localized string similar to Shortcut placed successfully..
/// Looks up a localized string similar to Shortcut successfully saved to &apos;{0}&apos;..
/// </summary>
internal static string Shortcut_placed_successfully {
get {

View File

@ -207,8 +207,8 @@
<data name="Executable_file_not_found" xml:space="preserve">
<value>Executable file not found.</value>
</data>
<data name="Executing_application_with_profile" xml:space="preserve">
<value>Executing '{0}' with '{1}' profile.</value>
<data name="Execute_application_with_profile" xml:space="preserve">
<value>Running '{0}' with '{1}' profile.</value>
</data>
<data name="Steam_is_not_installed" xml:space="preserve">
<value>Steam is not installed.</value>
@ -217,7 +217,7 @@
<value>Switching display profile to '{0}'.</value>
</data>
<data name="Shortcut_placed_successfully" xml:space="preserve">
<value>Shortcut placed successfully.</value>
<value>Shortcut successfully saved to '{0}'.</value>
</data>
<data name="Shortcut" xml:space="preserve">
<value>Shortcut</value>

View File

@ -19,6 +19,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using NvAPIWrapper.Native.Display.Structures;
using System.Text.RegularExpressions;
using IWshRuntimeLibrary;
namespace HeliosPlus
{
@ -531,7 +532,7 @@ namespace HeliosPlus
if (Category == ShortcutCategory.Application)
{
// Prepare text for the shortcut description field
shortcutDescription = string.Format(Language.Executing_application_with_profile, programName, Name);
shortcutDescription = string.Format(Language.Execute_application_with_profile, programName, ProfileToUse.Name);
}
// Only add the rest of the options if the temporary switch radio button is set
@ -539,7 +540,7 @@ namespace HeliosPlus
else if (Permanence == ShortcutPermanence.Temporary)
{
// Prepare text for the shortcut description field
shortcutDescription = string.Format(Language.Executing_application_with_profile, GameName, Name);
shortcutDescription = string.Format(Language.Execute_application_with_profile, GameName, ProfileToUse.Name);
}
}
@ -547,70 +548,58 @@ namespace HeliosPlus
else
{
// Prepare text for the shortcut description field
shortcutDescription = string.Format(Language.Switching_display_profile_to_profile, Name);
shortcutDescription = string.Format(Language.Switching_display_profile_to_profile, ProfileToUse.Name);
}
// Now we are ready to create a shortcut based on the filename the user gave us
shortcutFileName = Path.ChangeExtension(shortcutFileName, @"lnk");
// And we use the Icon from the shortcutIconCache
shortcutIconFileName = SavedShortcutIconCacheFilename;
// If the user supplied a file
if (shortcutFileName != null)
{
try
{
// Remove the old file to replace it
if (File.Exists(shortcutFileName))
// Remove the old file if it exists to replace it
if (System.IO.File.Exists(shortcutFileName))
{
File.Delete(shortcutFileName);
System.IO.File.Delete(shortcutFileName);
}
// Actually create the shortcut!
var wshShellType = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
dynamic wshShell = Activator.CreateInstance(wshShellType);
//var wshShellType = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
//dynamic wshShell = Activator.CreateInstance(wshShellType);
try
{
var shortcut = wshShell.CreateShortcut(shortcutFileName);
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutFileName);
try
{
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Arguments = string.Join(" ", shortcutArgs);
shortcut.Description = shortcutDescription;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath) ??
string.Empty;
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Arguments = string.Join(" ", shortcutArgs);
shortcut.Description = shortcutDescription;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath) ??
string.Empty;
shortcut.IconLocation = shortcutIconFileName;
shortcut.Save();
}
finally
{
Console.WriteLine($"ShortcutItem/CreateShortcut exception (saving)");
Marshal.FinalReleaseComObject(shortcut);
}
}
finally
{
Console.WriteLine($"ShortcutItem/CreateShortcut exception (Creating shortcut)");
Marshal.FinalReleaseComObject(wshShell);
}
shortcut.IconLocation = shortcutIconFileName;
shortcut.Save();
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutItem/CreateShortcut exception (deleting old shortcut)");
// Clean up a failed attempt
if (File.Exists(shortcutFileName))
if (System.IO.File.Exists(shortcutFileName))
{
File.Delete(shortcutFileName);
System.IO.File.Delete(shortcutFileName);
}
}
}
// Return a status on how it went
// true if it was a success or false if it was not
return shortcutFileName != null && File.Exists(shortcutFileName);
return shortcutFileName != null && System.IO.File.Exists(shortcutFileName);
}
public void AutoSuggestShortcutName()

View File

@ -15,7 +15,7 @@ using System.Windows.Forms;
namespace HeliosPlus
{
class ShortcutRepository
public static class ShortcutRepository
{
#region Class Variables
// Common items to the class
@ -24,33 +24,15 @@ namespace HeliosPlus
// Other constants that are useful
private static string _shortcutStorageJsonPath = Path.Combine(Program.AppDataPath, $"Shortcuts");
private static string _shortcutStorageJsonFileName = Path.Combine(_shortcutStorageJsonPath, $"Shortcuts_{Version.ToString(2)}.json");
private static uint _lastShortcutId;
#endregion
#region Instance Variables
// Individual items per class instance
#endregion
#region Class Constructors
public ShortcutRepository()
static ShortcutRepository()
{
// Load the Shortcuts from storage
LoadShortcuts();
/* if (LoadShortcuts() && ShortcutCount > 0)
{
// Work out the starting NextShortcutId value
long max = _allShortcuts.Max<ShortcutItem>(item => item.Id);
_lastShortcutId = Convert.ToUInt32(max);
} else
_lastShortcutId = 0;
*/ }
public ShortcutRepository(ShortcutItem shortcut) : this()
{
if (shortcut is ShortcutItem)
AddShortcut(shortcut);
}
#endregion
#region Class Properties

View File

@ -19,13 +19,11 @@ namespace HeliosPlus.UIForms
//private static bool _inDialog = false;
private static ProfileItem _profileToLoad = null;
private ProfileAdaptor _profileAdaptor = new ProfileAdaptor();
private ProfileRepository _profileRepository = new ProfileRepository();
public DisplayProfileForm()
{
InitializeComponent();
this.AcceptButton = this.btn_save_or_rename;
//_profileRepository = new ProfileRepository();
}
public DisplayProfileForm(ProfileItem profileToLoad) : this()

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.btn_setup_display_profiles = new System.Windows.Forms.Button();
@ -36,6 +37,7 @@
this.btn_setup_game_shortcuts = new System.Windows.Forms.Button();
this.btn_exit = new System.Windows.Forms.Button();
this.pb_game_shortcut = new System.Windows.Forms.PictureBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@ -115,6 +117,10 @@
this.pb_game_shortcut.TabStop = false;
this.pb_game_shortcut.Click += new System.EventHandler(this.pb_game_shortcut_Click);
//
// notifyIcon1
//
resources.ApplyResources(this.notifyIcon1, "notifyIcon1");
//
// MainForm
//
resources.ApplyResources(this, "$this");
@ -144,5 +150,6 @@
private System.Windows.Forms.Button btn_setup_display_profiles;
private System.Windows.Forms.Button btn_setup_game_shortcuts;
private System.Windows.Forms.Label lbl_version;
private System.Windows.Forms.NotifyIcon notifyIcon1;
}
}

View File

@ -58,5 +58,6 @@ namespace HeliosPlus.UIForms
// Start loading the Steam Games just after the Main form opens
SteamGame.GetAllInstalledGames();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,6 @@ namespace HeliosPlus.UIForms
List<SteamGame> _allSteamGames;
private ProfileAdaptor _profileAdaptor;
//private List<ProfileItem> _loadedProfiles = new List<ProfileItem>();
private ProfileRepository _profileRepository;
private ProfileItem _profileToUse= null;
private ShortcutItem _shortcutToEdit = null;
private bool _isNewShortcut = false;
@ -39,8 +38,6 @@ namespace HeliosPlus.UIForms
// Set the profileAdaptor we need to load images from Profiles
// into the Profiles ImageListView
_profileAdaptor = new ProfileAdaptor();
// Then load the ProfilesRepository
_profileRepository = new ProfileRepository();
// Create a new SHortcut if we are creating a new one
// And set up the page (otherwise this is all set when we load an

View File

@ -158,6 +158,7 @@
this.btn_save.TabIndex = 30;
this.btn_save.Text = "&Save to Desktop";
this.btn_save.UseVisualStyleBackColor = false;
this.btn_save.Click += new System.EventHandler(this.btn_save_Click);
//
// dialog_save
//

View File

@ -23,8 +23,6 @@ namespace HeliosPlus.UIForms
private ShortcutAdaptor _shortcutAdaptor = new ShortcutAdaptor();
private ImageListViewItem _selectedShortcutILVItem = null;
private ShortcutItem _selectedShortcut = null;
private ShortcutRepository _shortcutRepository = new ShortcutRepository();
private ProfileRepository _profileRepository = new ProfileRepository();
public ShortcutLibraryForm()
{
@ -32,7 +30,7 @@ namespace HeliosPlus.UIForms
//_shortcutAdaptor = new ShortcutAdaptor();
//_shortcutRepository = new ShortcutRepository();
//_profileRepository = new ProfileRepository();
}
}
private void btn_new_Click(object sender, EventArgs e)
{
@ -40,7 +38,7 @@ namespace HeliosPlus.UIForms
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
_selectedShortcut = shortcutForm.Shortcut;
ShortcutRepository.AddShortcut(shortcutForm.Shortcut);
RefreshShortcutLibraryUI();
}
}
@ -65,18 +63,15 @@ namespace HeliosPlus.UIForms
ilv_saved_shortcuts.SuspendLayout();
ImageListViewItem newItem = null;
ilv_saved_shortcuts.Items.Clear();
foreach (ShortcutItem loadedShortcut in ShortcutRepository.AllShortcuts)
{
bool thisLoadedShortcutIsAlreadyHere = (from item in ilv_saved_shortcuts.Items where item.Text == loadedShortcut.Name select item.Text).Any();
if (!thisLoadedShortcutIsAlreadyHere)
{
//loadedProfile.SaveProfileImageToCache();
//newItem = new ImageListViewItem(loadedProfile.SavedProfileCacheFilename, loadedProfile.Name);
//newItem = new ImageListViewItem(loadedProfile, loadedProfile.Name);
newItem = new ImageListViewItem(loadedShortcut, loadedShortcut.Name);
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
}
//loadedProfile.SaveProfileImageToCache();
//newItem = new ImageListViewItem(loadedProfile.SavedProfileCacheFilename, loadedProfile.Name);
//newItem = new ImageListViewItem(loadedProfile, loadedProfile.Name);
newItem = new ImageListViewItem(loadedShortcut, loadedShortcut.Name);
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
}
if (_selectedShortcut != null && _selectedShortcut is ShortcutItem)
@ -135,7 +130,7 @@ namespace HeliosPlus.UIForms
}
else
{
dialog_save.FileName = String.Concat(_selectedShortcut, @" (", _selectedShortcut.Name, @")");
dialog_save.FileName = _selectedShortcut.Name;
}
}
@ -145,7 +140,7 @@ namespace HeliosPlus.UIForms
if (_selectedShortcut.CreateShortcut(dialog_save.FileName))
{
MessageBox.Show(
Language.Shortcut_placed_successfully,
String.Format(Language.Shortcut_placed_successfully, dialog_save.FileName),
Language.Shortcut,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
@ -186,7 +181,6 @@ namespace HeliosPlus.UIForms
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
_selectedShortcut = shortcutForm.Shortcut;
RefreshShortcutLibraryUI();
}
@ -201,7 +195,6 @@ namespace HeliosPlus.UIForms
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
_selectedShortcut = shortcutForm.Shortcut;
RefreshShortcutLibraryUI();
}