Ejemplo n.º 1
0
        public bool LaunchProcess(string args, CommandDialog cd, bool bShowProgress = false, string sDestination = "")
        {
            try
            {
                strMessage = string.Empty;
                Process build = new Process();

                Thread thDialog = null;
                if (bShowProgress)
                {
                    if (cd == CommandDialog.Copy)
                    {
                        if (pf == null)
                        {
                            pf = new ProgressForm();
                        }
                        pf.SetProcess(build);
                        pf.SetProgress(string.Empty, 0, sDestination);
                        thDialog = new Thread(() => ThreadProcCopy());
                        thDialog.Start();
                    }
                    else if (cd == CommandDialog.Connect)
                    {
                        if (cf == null)
                        {
                            cf = new ConnectForm();
                        }
                        DeviseAddr addr = new DeviseAddr();
                        if (!string.IsNullOrEmpty(addr.ConnectionType) && addr.ConnectionType.Equals("usb") &&
                            !string.IsNullOrEmpty(addr.UsbDevice))
                        {
                            cf.SetIP(addr.UsbDevice);
                        }
                        else
                        {
                            cf.SetIP(addr.ToString());
                        }

                        thDialog = new Thread(() => ThreadProcConnect());
                        thDialog.Start();
                    }
                    else if (cd == CommandDialog.CreateScreenShot)
                    {
                        if (of == null)
                        {
                            of = new OperationForm(OperationForm.TypeOp.CreateScreenShot);
                        }
                        of.SetOperation("Creating ScreenShot");
                        thDialog = new Thread(() => ThreadProcOperation());
                        thDialog.Start();
                    }
                    else if (cd == CommandDialog.Install)
                    {
                        if (of == null)
                        {
                            of = new OperationForm(OperationForm.TypeOp.Install);
                        }
                        of.SetOperation("Install " + args.Substring(args.LastIndexOf("\\") + 1).TrimEnd('\"'));
                        thDialog = new Thread(() => ThreadProcOperation());
                        thDialog.Start();
                    }
                    else if (cd == CommandDialog.Delete)
                    {
                        if (of == null)
                        {
                            of = new OperationForm(OperationForm.TypeOp.Delete);
                        }
                        of.SetOperation("Deleting");
                        thDialog = new Thread(() => ThreadProcOperation());
                        thDialog.Start();
                    }
                }

                build.StartInfo.WorkingDirectory = Path.GetDirectoryName(FindADB());

                if (string.IsNullOrEmpty(build.StartInfo.WorkingDirectory))
                {
                    if (bShowProgress && pf != null)
                    {
                        pf.SetProgress(string.Empty, 0);
                        pf.InternalCloseDialog();
                        pf = null;
                    }
                    if (bShowProgress && cf != null)
                    {
                        cf.InternalCloseDialog();
                        cf = null;
                    }
                    if (bShowProgress && of != null)
                    {
                        of.InternalCloseDialog();
                        of = null;
                    }
                    strMessage = "Can not find adb.exe";
                    return(false);
                }

                build.StartInfo.FileName  = FindADB();
                build.StartInfo.Arguments = args;

                build.StartInfo.UseShellExecute        = false;
                build.StartInfo.RedirectStandardOutput = true;
                build.StartInfo.RedirectStandardError  = true;
                build.StartInfo.CreateNoWindow         = true;
                build.ErrorDataReceived  += build_ErrorDataReceived;
                build.OutputDataReceived += build_ErrorDataReceived;
                build.EnableRaisingEvents = true;
                build.Start();
                build.BeginOutputReadLine();
                build.BeginErrorReadLine();
                build.WaitForExit();

                if (bShowProgress)
                {
                    if (cd == CommandDialog.Copy)
                    {
                        if (build.ExitCode < 0)
                        {
                            if (!string.IsNullOrEmpty(sDestination))
                            {
                                if (File.Exists(sDestination))
                                {
                                    File.Delete(sDestination);
                                }
                                else if (Directory.Exists(sDestination))
                                {
                                    //Directory.Delete(sDestination, true);
                                }
                            }
                        }

                        if (pf != null)
                        {
                            pf.SetProgress(string.Empty, 0);
                            pf.InternalCloseDialog();
                            pf = null;
                        }
                    }
                    else if (cd == CommandDialog.Connect)
                    {
                        cf.InternalCloseDialog();
                        cf = null;
                    }
                    else if (cd == CommandDialog.CreateScreenShot || cd == CommandDialog.Delete || cd == CommandDialog.Install)
                    {
                        of.InternalCloseDialog();
                        of = null;
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                if (bShowProgress)
                {
                    if (cd == CommandDialog.Copy)
                    {
                        if (pf != null)
                        {
                            pf.SetProgress(string.Empty, 0);
                            pf.InternalCloseDialog();
                            pf = null;
                        }
                    }
                    else if (cd == CommandDialog.Connect)
                    {
                        cf.InternalCloseDialog();
                        cf = null;
                    }
                    else if (cd == CommandDialog.CreateScreenShot || cd == CommandDialog.Delete)
                    {
                        of.InternalCloseDialog();
                        of = null;
                    }
                }

                strMessage = e.Message;
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool LaunchProcess(List <FileOperation> lstItems, CommandDialog cd, bool bShowProgress = false)
        {
            string sAdb = FindADB();

            if (string.IsNullOrEmpty(sAdb))
            {
                strMessage = "Can not find adb.exe";
                return(false);
            }

            Thread thCopy = null;

            if (bShowProgress)
            {
                if (cd == CommandDialog.Copy)
                {
                    if (pf == null)
                    {
                        pf       = new ProgressForm();
                        pf.Files = lstItems;
                    }

                    thCopy = new Thread(() => ThreadProcCopy());
                    thCopy.Start();
                }
                if (cd == CommandDialog.Delete)
                {
                    if (of == null)
                    {
                        of = new OperationForm(OperationForm.TypeOp.Delete);
                        of.SetOperation("Deleting");
                    }

                    thCopy = new Thread(() => ThreadProcOperation());
                    thCopy.Start();
                }
            }

            int  iFile = 0;
            bool bRet  = true;

            foreach (FileOperation fileOperation in lstItems)
            {
                Process build = new Process();

                if (cd == CommandDialog.Copy)
                {
                    if (bShowProgress && pf != null)
                    {
                        pf.SetProgressVisible(false);
                        pf.CurrentPosition = iFile;
                        pf.SetProcess(build, fileOperation.IsFolder ? "1" : "0");
                        pf.SetProgress(fileOperation.Source, 0, fileOperation.Destination);
                    }
                }

                build.StartInfo.WorkingDirectory = Path.GetDirectoryName(sAdb);
                build.StartInfo.FileName         = sAdb;
                build.StartInfo.Arguments        = fileOperation.Command;

                build.StartInfo.UseShellExecute        = false;
                build.StartInfo.RedirectStandardOutput = true;
                build.StartInfo.RedirectStandardError  = true;
                build.StartInfo.CreateNoWindow         = true;
                build.ErrorDataReceived  += build_ErrorDataReceived;
                build.OutputDataReceived += build_ErrorDataReceived;
                build.EnableRaisingEvents = true;
                build.Start();
                build.BeginOutputReadLine();
                build.BeginErrorReadLine();
                build.WaitForExit();

                if (bShowProgress)
                {
                    if (cd == CommandDialog.Copy)
                    {
                        if (build.ExitCode < 0)
                        {
                            if (!string.IsNullOrEmpty(fileOperation.Destination))
                            {
                                if (File.Exists(fileOperation.Destination))
                                {
                                    File.Delete(fileOperation.Destination);
                                }
                                else if (Directory.Exists(fileOperation.Destination))
                                {
                                    //Directory.Delete(sDestination, true);
                                }
                            }

                            if (pf != null)
                            {
                                pf.SetProgress(string.Empty, 0);
                                pf.InternalCloseDialog();
                                pf = null;
                            }

                            break;
                        }
                        else
                        {
                            if (pf != null)
                            {
                                pf.SetProgress(string.Empty, 0);
                            }
                        }
                    }
                }

                if (strMessage != null && (strMessage.Contains("adb: error:") || strMessage.Contains("Read-only file system") || strMessage.Contains(": Permission denied")))
                {
                    bRet = false;
                    break;
                }

                iFile++;
            }

            if (bShowProgress)
            {
                if (cd == CommandDialog.Copy)
                {
                    if (pf != null)
                    {
                        pf.SetProgress(string.Empty, 0);
                        pf.InternalCloseDialog();
                        pf = null;
                    }
                }
                if (cd == CommandDialog.Delete)
                {
                    if (of != null)
                    {
                        of.InternalCloseDialog();
                        of = null;
                    }
                }
            }

            return(bRet);
        }