Exemplo n.º 1
0
        int Run(string[] args)
        {
            var failureCount = 0;

            if (args.Length > 0)
            {
                MessageBox.Show(args[0]);
            }
            else
            {
                var scheme          = StringToLowerInvariant(GetType().Name);
                var applicationPath = new Uri(entryAssemblyName.CodeBase).LocalPath;
                using (var protocolRegistrar = new ProtocolRegistrar())
                {
                    if (PromptYesNo(string.Format("Register {0} for protocol {1}?", applicationPath, scheme)))
                    {
                        if (!protocolRegistrar.Register(ProtocolRegistryScope.User, scheme, applicationPath))
                        {
                            MessageBox.Show("Registering failed.", entryAssemblyName.Name, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                            failureCount++;
                        }
                    }
                    if (PromptYesNo(string.Format("Unregister protocol {1}?", applicationPath, scheme)))
                    {
                        if (!protocolRegistrar.Unregister(ProtocolRegistryScope.User, scheme))
                        {
                            MessageBox.Show("Unregistering failed.");
                            failureCount++;
                        }
                    }
                }
            }
            return(failureCount > 0 ? 1 : 0);
        }
        protected bool Elevate(bool register, string scheme, string applicationPath = null)
        {
            var processStartInfo = new ProcessStartInfo(
                GetAssemblyPath(Assembly.GetCallingAssembly()),
                "register " + ProtocolRegistrar.ArgEncode(scheme) + " " + ProtocolRegistrar.ArgEncode(applicationPath));

            using (var process = Process.Start(processStartInfo))
            {
                process.WaitForExit();
                System.Windows.Forms.MessageBox.Show("" + process.ExitCode);
                return(process.ExitCode == 0);
            }
        }
        public static int Main(string[] args)
        {
            if (ArgDecode(ArgEncode("a!b\\")) != "a!b\\")
            {
                throw new Exception("POST fail");
            }
            var _register   = "register";
            var _unregister = "unregister";
            var optind      = 0;

            if (args.Length <= optind ||
                !((ICollection <string>) new [] { _register, _unregister, }).Contains(args[optind]))
            {
                return(badArgs());
            }
            var register = args[optind++] == _register;

            var scheme = args.Length > optind?ArgDecode(args[optind++]) : null;

            if (scheme == null)
            {
                return(badArgs());
            }

            var applicationPath = args.Length > optind?ArgDecode(args[optind++]) : null;

            if (applicationPath == null)
            {
                return(badArgs());
            }

            // Assume that invocation through Main() indicates we have
            // elevated, perhaps through ElevateProtocolRegistrar()
            // which just tries to invoke us directly.
            using (var protocolRegistrar = new ProtocolRegistrar(false))
            {
                if (register)
                {
                    Console.Error.WriteLine("Attempting to register scheme “{0}:” to launch “{1}”.", scheme, applicationPath);
                    return(protocolRegistrar.Register(ProtocolRegistryScope.User, scheme, applicationPath) ? 0 : 1);
                }
                else
                {
                    Console.Error.WriteLine("Attempting to unregister scheme “{0}:”.", scheme);
                    return(protocolRegistrar.Unregister(ProtocolRegistryScope.User, scheme) ? 0 : 1);
                }
            }
        }