Add possibility to register / unregister file converter shell extension directly from File Converter in command line.

This commit is contained in:
Adrien Allard committed 2019-04-07 18:25:04 +02:00
1 parent c58986c29a
commit 248caa4c8f
6 files changed
+196 -38

No files matched your search

+77 -24
View File
@@ -211,16 +211,7 @@ namespace FileConverter
// Retrieve arguments.
Debug.Log("Retrieve arguments...");
string[] args = Environment.GetCommandLineArgs();
#if (DEBUG)
{
System.Array.Resize(ref args, 3);
args[1] = "--settings";
args[2] = "--verbose";
}
#endif
// Log arguments.
for (int index = 0; index < args.Length; index++)
{
@@ -240,6 +231,8 @@ namespace FileConverter
ISettingsService settingsService = SimpleIoc.Default.GetInstance<ISettingsService>();
// Parse arguments.
bool quitAfterStartup = false;
int quitExitCode = 0;
List<string> filePaths = new List<string>();
string conversionPresetName = null;
for (int index = 1; index < args.Length; index++)
@@ -258,14 +251,65 @@ namespace FileConverter
switch (parameterTitle)
{
case "post-install-init":
settingsService.PostInstallationInitialization();
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
return;
if (!settingsService.PostInstallationInitialization())
{
quitExitCode = 0x0F;
Debug.LogError(quitExitCode, $"Failed to execute post install initialization.");
}
quitAfterStartup = true;
break;
case "register-shell-extension":
{
quitAfterStartup = true;
if (index >= args.Length - 1)
{
quitExitCode = 0x0B;
Debug.LogError(quitExitCode, $"Invalid format.");
break;
}
string shellExtensionPath = args[index + 1];
index++;
if (!Helpers.RegisterShellExtension(shellExtensionPath))
{
quitExitCode = 0x0C;
Debug.LogError(quitExitCode, $"Failed to register shell extension {shellExtensionPath}.");
}
break;
}
case "unregister-shell-extension":
{
quitAfterStartup = true;
if (index >= args.Length - 1)
{
quitExitCode = 0x0D;
Debug.LogError(quitExitCode, $"Invalid format.");
break;
}
string shellExtensionPath = args[index + 1];
index++;
if (!Helpers.UnregisterExtension(shellExtensionPath))
{
quitExitCode = 0x0E;
Debug.LogError(quitExitCode, $"Failed to unregister shell extension {shellExtensionPath}.");
}
break;
}
case "version":
Console.Write(ApplicationVersion.ToString());
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
return;
Console.WriteLine(ApplicationVersion.ToString());
quitAfterStartup = true;
break;
case "settings":
this.showSettings = true;
@@ -273,20 +317,21 @@ namespace FileConverter
case "apply-settings":
settingsService.ApplyTemporarySettings();
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
return;
quitAfterStartup = true;
break;
case "conversion-preset":
if (index >= args.Length - 1)
{
Debug.LogError("Invalid format. (code 0x01)");
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
return;
quitAfterStartup = true;
quitExitCode = 0x01;
Debug.LogError(quitExitCode, $"Invalid format.");
break;
}
conversionPresetName = args[index + 1];
index++;
continue;
break;
case "verbose":
{
@@ -308,10 +353,18 @@ namespace FileConverter
if (settingsService.Settings == null)
{
Diagnostics.Debug.LogError("The application will now shutdown. If you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Debug.LogError("Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
quitAfterStartup = true;
quitExitCode = 13;
}
if (quitAfterStartup)
{
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(quitExitCode)));
return;
}
Debug.Assert(quitExitCode == 0, "An error happened during the initialization.");
// Check for upgrade.
if (settingsService.Settings.CheckUpgradeAtStartup)
+14 -2
View File
@@ -106,8 +106,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Middleware\Ripper.dll</HintPath>
</Reference>
<Reference Include="SharpShell, Version=2.7.1.0, Culture=neutral, PublicKeyToken=f14dc899472fe6fb, processorArchitecture=MSIL">
<HintPath>..\..\packages\SharpShell.2.7.1\lib\net40-client\SharpShell.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmLightLibs.5.4.1.1\lib\net45\System.Windows.Interactivity.dll</HintPath>
</Reference>
@@ -335,7 +340,9 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="app.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\ApplicationIcon.png" />
@@ -379,7 +386,12 @@
<ItemGroup>
<Resource Include="Resources\SayThanks-Icon.png" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\FileConverterExtension\FileConverterExtension.csproj">
<Project>{0c44ca69-42d6-4357-bdfd-83069d1aba2f}</Project>
<Name>FileConverterExtension</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy /Y "$(SolutionDir)Middleware\ffmpeg\$(PlatformName)\ffmpeg.exe" "$(TargetDir)ffmpeg.exe"
+87 -2
View File
@@ -1,4 +1,4 @@
// // <copyright file="Helpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
// <copyright file="Helpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverter
{
@@ -6,19 +6,104 @@ namespace FileConverter
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.ComponentModel.Composition.Hosting;
using FileConverter.ConversionJobs;
using FileConverter.Services;
using SharpShell;
using SharpShell.ServerRegistration;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Win32;
public static class Helpers
{
public static bool RegisterShellExtension(string shellExtensionPath)
{
if (!Application.IsInAdmininstratorPrivileges)
{
Diagnostics.Debug.LogError("File Converter needs administrator privileges to register the shell extension.");
return false;
}
if (!File.Exists(shellExtensionPath))
{
Diagnostics.Debug.LogError($"Shell extension {shellExtensionPath} does not exists.");
return false;
}
Diagnostics.Debug.Log($"Install and register shell extension: {shellExtensionPath}.");
try
{
var catalog = new AssemblyCatalog(shellExtensionPath);
var container = new CompositionContainer(catalog);
var server = container.GetExport<ISharpShellServer>().Value;
RegistrationType registrationType = RegistrationType.OS64Bit;
#if BUILD32
registrationType = RegistrationType.OS32Bit;
#endif
ServerRegistrationManager.InstallServer(server, registrationType, true);
Diagnostics.Debug.Log($"Shell extension has been installed correctly.");
ServerRegistrationManager.RegisterServer(server, registrationType);
Diagnostics.Debug.Log($"Shell extension has been registered correctly.");
}
catch (Exception exception)
{
Diagnostics.Debug.LogError($"An exception has been thrown during shell extension {shellExtensionPath} registration.\n{exception}");
return false;
}
return true;
}
public static bool UnregisterExtension(string shellExtensionPath)
{
if (!Application.IsInAdmininstratorPrivileges)
{
Diagnostics.Debug.LogError("File Converter needs administrator privileges to unregister the shell extension.");
return false;
}
if (!File.Exists(shellExtensionPath))
{
Diagnostics.Debug.LogError($"Shell extension {shellExtensionPath} does not exists.");
return false;
}
Diagnostics.Debug.Log($"Unregister and uninstall shell extension: {shellExtensionPath}.");
try
{
var catalog = new AssemblyCatalog(shellExtensionPath);
var container = new CompositionContainer(catalog);
var server = container.GetExport<ISharpShellServer>().Value;
RegistrationType registrationType = RegistrationType.OS64Bit;
#if BUILD32
registrationType = RegistrationType.OS32Bit;
#endif
ServerRegistrationManager.UnregisterServer(server, registrationType);
Diagnostics.Debug.Log($"Shell extension has been successfully unregistered.");
ServerRegistrationManager.UninstallServer(server, registrationType);
Diagnostics.Debug.Log($"Shell extension has been successfully uninstalled.");
}
catch (Exception exception)
{
Diagnostics.Debug.LogError($"An exception has been thrown during shell extension {shellExtensionPath} unregistration.\n{exception}");
return false;
}
return true;
}
public static IEnumerable<CultureInfo> GetSupportedCultures()
{
// Get all cultures.
@@ -9,7 +9,7 @@ namespace FileConverter.Services
get;
}
void PostInstallationInitialization();
bool PostInstallationInitialization();
void ApplyTemporarySettings();
@@ -72,8 +72,10 @@ namespace FileConverter.Services
}
}
public void PostInstallationInitialization()
public bool PostInstallationInitialization()
{
Debug.Log("Execute post installation initialization.");
Settings defaultSettings = null;
// Load the default settings.
@@ -85,12 +87,14 @@ namespace FileConverter.Services
}
catch (Exception exception)
{
Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
Debug.LogError($"Fail to load file converter default settings. {exception.Message}");
return false;
}
}
else
{
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", this.DefaultSettingsFilePath);
return false;
}
// Load user settings if exists.
@@ -131,7 +135,7 @@ namespace FileConverter.Services
}
Settings settings = userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings;
this.Save(settings);
return this.Save(settings);
}
public void ApplyTemporarySettings()
@@ -148,7 +152,7 @@ namespace FileConverter.Services
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter");
if (registryKey == null)
{
Debug.LogError("Can't apply settings in registry. (code 0x03)");
Debug.LogError(0x03, "Can't apply settings in registry.");
return;
}
@@ -241,7 +245,7 @@ namespace FileConverter.Services
return settings;
}
private void Save(Settings settings)
private bool Save(Settings settings)
{
if (settings == null)
{
@@ -279,7 +283,8 @@ namespace FileConverter.Services
}
else
{
MessageBox.Show("Can't apply settings in registry. (code 0x09)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.LogError(0x09, "Can't apply settings in registry.");
return false;
}
}
}
@@ -289,6 +294,8 @@ namespace FileConverter.Services
File.Copy(this.UserSettingsTemporaryFilePath, this.UserSettingsFilePath, true);
File.Delete(this.UserSettingsTemporaryFilePath);
}
return true;
}
private void RunSaveInAdminMode()
@@ -374,7 +381,7 @@ namespace FileConverter.Services
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter", RegistryKeyPermissionCheck.ReadWriteSubTree);
if (registryKey == null)
{
MessageBox.Show("Can't apply settings in registry. (code 0x05)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.LogError(0x05, "Can't find File Converter registry entry.");
return false;
}
@@ -388,7 +395,7 @@ namespace FileConverter.Services
}
catch (Exception)
{
MessageBox.Show("Can't apply settings in registry. (code 0x06)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.LogError(0x06, "Can't apply settings in registry.");
return false;
}
}
@@ -419,7 +426,7 @@ namespace FileConverter.Services
if (subKey == null)
{
MessageBox.Show("Can't apply settings in registry. (code 0x07)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.LogError(0x07, "Can't apply settings in registry.");
return false;
}
@@ -8,5 +8,6 @@
<package id="MvvmLight" version="5.4.1.1" targetFramework="net45" />
<package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net45" />
<package id="Office" version="12.0.0" targetFramework="net45" />
<package id="SharpShell" version="2.7.1" targetFramework="net461" />
<package id="WpfAnimatedGif" version="1.4.14" targetFramework="net45" />
</packages>