BExplorer.Shell.AssociationItem.Invoke C# (CSharp) Method

Invoke() public method

Invokes/Runs the application this item represents and supplies it with the owner
public Invoke ( ) : void
return void
		public void Invoke() {
			RegistryKey root = Registry.ClassesRoot;
			var applications = root.OpenSubKey("Applications", RegistryKeyPermissionCheck.ReadSubTree);
			var currentApplication = applications.OpenSubKey(Path.GetFileName(this.ApplicationPath), RegistryKeyPermissionCheck.ReadSubTree);

			Action OnFail = () => {
				Process.Start(this.ApplicationPath, "\"" + this.Owner.ParsingName + "\"");
				applications.Close();
				root.Close();
			};

			if (currentApplication == null) {
				OnFail();
				return;
			}

			var command = currentApplication.OpenSubKey(@"shell\open\command", RegistryKeyPermissionCheck.ReadSubTree);
			if (command == null) {
				command = currentApplication.OpenSubKey(@"shell\edit\command", RegistryKeyPermissionCheck.ReadSubTree);
			}

			if (command == null) {
				OnFail();
				return;
			}

			var commandValue = command.GetValue("").ToString();
			var argsArray = Shell32.CommandLineToArgs(commandValue).ToList();
			var executable = argsArray[0];
			argsArray.RemoveAt(0);
			for (int i = 0; i < argsArray.Count; i++) {
				if (argsArray[i] != "%1" && argsArray[i] != "%L" && argsArray[i].ToLowerInvariant().Contains("photoviewer.dll")) {
					argsArray[i] = "\"" + argsArray[i].Replace(",", "") + "\",";
				}
				else if ((argsArray[i] == "%1" || argsArray[i] == "%L") && !argsArray[i].ToLowerInvariant().Contains("photoviewer.dll")) {
					argsArray[i] = "\"" + argsArray[i] + "\"";
				}
			}

			var args = String.Join(" ", argsArray).Replace("%1", this.Owner.ParsingName).Replace("%L", this.Owner.ParsingName).Replace(@"\\", @"\");
			commandValue = Environment.ExpandEnvironmentVariables(commandValue);
			Process.Start(executable, args);

			command.Close();
			currentApplication.Close();
			applications.Close();
			root.Close();
		}
	}