public static bool TryExecuteApplicationOperation(DevicePortal portal, ParameterHelper parameters)
 {
     try
     {
         var appOperation = new AppOperation(portal);
         appOperation.ExecuteOperation(AppOperation.OperationStringToEnum(parameters.GetParameterValue(ParameterHelper.Operation)), parameters);
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
        private static ProgramErrorCodes ExecuteMain(string[] args)
        {
            ParameterHelper   parameters;
            ProgramErrorCodes errorCode;
            Uri  targetDevice = null;
            bool helpFlag;
            bool verbose;

            errorCode = ParseParametersAndPerformBasicValidation(args, out parameters, out targetDevice, out verbose, out helpFlag);
            if (errorCode != ProgramErrorCodes.Success)
            {
                return(errorCode);
            }

            if (!helpFlag && parameters.HasFlag(ParameterHelper.StdinCredentials))
            {
                try
                {
                    string username;
                    string password;

                    if (!TryReadCredentialsFromStdin(out username, out password))
                    {
                        Console.Out.WriteLine("Failed to read WDP credentials from stdin");
                        Console.Out.WriteLine();

                        return(ProgramErrorCodes.InvalidParameters);
                    }

                    parameters.AddOrUpdateParameter(ParameterHelper.WdpUser, username);
                    parameters.AddOrUpdateParameter(ParameterHelper.WdpPassword, password);
                }
                catch (Exception ex)
                {
                    Console.Out.WriteLine("Fatal error reading WDP credentials from stdin: " + ex.Message);
                    Console.Out.WriteLine();

                    return(ProgramErrorCodes.UnexpectedError);
                }
            }

            DevicePortal portal;

            if (!helpFlag)
            {
                try
                {
                    if (!TryOpenDevicePortalConnection(targetDevice, parameters, out portal))
                    {
                        if (portal != null && portal.ConnectionHttpStatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            Console.Out.WriteLine("Aborting due to failed authentication");
                            Console.Out.WriteLine();
                            return(ProgramErrorCodes.AuthenticationFailed);
                        }
                        else
                        {
                            Console.Out.WriteLine("Aborting due to failed connection with remote device");
                            Console.Out.WriteLine();
                            return(ProgramErrorCodes.ConnectionFailed);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Out.WriteLine("Fatal error initializing DevicePortal: " + ex.Message);
                    Console.Out.WriteLine();

                    return(ProgramErrorCodes.UnexpectedError);
                }
            }
            else
            {
                // Need to create a dummy DevicePortal so can safely call into area-specific operation processor
                // The processor can then output operation specific help info
                portal = new DevicePortal(new DefaultDevicePortalConnection("http://0.0.0.0", "", ""));
            }

            var operationResult = ProgramErrorCodes.Success;

            switch (parameters.Area)
            {
            case OpperationArea.Application:
                if (!AppOperation.TryExecuteApplicationOperation(portal, parameters))
                {
                    operationResult = ProgramErrorCodes.OperationFailed;
                }
                break;

            default:
                // This case should have already been handled by a parameter check above
                operationResult = ProgramErrorCodes.InvalidParameters;
                break;
            }

            // If successful but help switch was set return a different resultCode
            if (operationResult == ProgramErrorCodes.Success && helpFlag)
            {
                operationResult = ProgramErrorCodes.SuccessHelp;
            }

            // If a debugger is attached, don't close but instead loop here until
            // closed.
            while (System.Diagnostics.Debugger.IsAttached)
            {
                System.Threading.Thread.Sleep(0);
            }

            return(operationResult);
        }