RIM.VSNDK_Package.VSNDK_PackagePackage.getPID C# (CSharp) Method

getPID() private method

Get the PID of the launched native app by parsing text from the output window.
private getPID ( DTE2 dte, string &pidString ) : bool
dte DTE2 Application Object.
pidString string Returns the Process ID as a string.
return bool
        private bool getPID(DTE2 dte, ref string pidString)
        {
            // Select all of the text
            _owP.TextDocument.Selection.SelectAll();
            string outputText = _owP.TextDocument.Selection.Text;

            // Check for successful deployment
            if (System.Text.RegularExpressions.Regex.IsMatch(outputText, "Info: done"))
            {
                string pattern = @"\s+result::(\d+)\r\n.+";
                Regex r = new Regex(pattern, RegexOptions.IgnoreCase);

                // Match the regular expression pattern against a text string.
                Match m = r.Match(outputText);

                // Take first match
                if (m.Success)
                {
                    Group g = m.Groups[1];
                    CaptureCollection cc = g.Captures;
                    Capture c = cc[0];
                    pidString = c.ToString();

                    // Store proccess name and file location into ProcessesPath.txt, so "Attach To Process" would be able to find the
                    // source code for a running process.
                    // First read the file.
                    string processesPaths = "";
                    System.IO.StreamReader readProcessesPathsFile = null;
                    try
                    {
                        readProcessesPathsFile = new System.IO.StreamReader(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Research In Motion\ProcessesPath.txt");
                        processesPaths = readProcessesPathsFile.ReadToEnd();
                        readProcessesPathsFile.Close();
                    }
                    catch (Exception e)
                    {
                        processesPaths = "";
                    }

                    // Updating the contents.
                    int begin = outputText.IndexOf("Deploy started: Project: ") + 25;
                    if (begin == -1)
                        begin = outputText.IndexOf("Project: ") + 9;
                    int end = outputText.IndexOf(", Configuration:", begin);
                    string processName = outputText.Substring(begin, end - begin) + "_" + _isSimulator.ToString();
                    begin = processesPaths.IndexOf(processName + ":>");

                    //                    string currentPath = dte.ActiveDocument.Path;
                    string currentPath = "";

                    foreach (string[] paths in _targetDir)
                    {
                        if (paths[0] == processName)
                        {
                            currentPath = paths[1];
                            break;
                        }
                    }

                    if (begin != -1)
                    {
                        begin += processName.Length + 2;
                        end = processesPaths.IndexOf("\r\n", begin);
                        processesPaths = processesPaths.Substring(0, begin) + currentPath + processesPaths.Substring(end);
                    }
                    else
                    {
                        processesPaths = processesPaths + processName + ":>" + currentPath + "\r\n";
                    }

                    // Writing contents to file.
                    System.IO.StreamWriter writeProcessesPathsFile = null;
                    try
                    {
                        writeProcessesPathsFile = new System.IO.StreamWriter(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Research In Motion\ProcessesPath.txt", false);
                        writeProcessesPathsFile.Write(processesPaths);
                        writeProcessesPathsFile.Close();
                    }
                    catch (Exception e)
                    {
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }