Johnny Lai

Johnny Lai

  • NA
  • 10
  • 1.9k

Trying to add some code into existing project

Mar 18 2017 1:15 PM
Hi,

I'm fighting to understand how to add code found on the internet correctly into a new project.

Let me give u an example here. If i Add some code i found on the internet it gives a lot of errors. The fist error is : The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)

Am I inserting the code in the wrong place. Please advice.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication2  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.   
  14. // THIS IS CODE FOUND ON THE INTERNET THAT I WANT TO TEST //  
  15. // START//  
  16.   
  17.             // there are always multiple chrome processes, so we have to loop through all of them to find the  
  18.             // process with a Window Handle and an automation element of name "Address and search bar"  
  19.             Process[] procsChrome = Process.GetProcessesByName("chrome");  
  20.             foreach (Process chrome in procsChrome)  
  21.             {  
  22.                 // the chrome process must have a window  
  23.                 if (chrome.MainWindowHandle == IntPtr.Zero)  
  24.                 {  
  25.                     continue;  
  26.                 }  
  27.   
  28.                 // find the automation element  
  29.                 AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);  
  30.   
  31.                 // manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)  
  32.                 AutomationElement elmUrlBar = null;  
  33.                 try  
  34.                 {  
  35.                     // walking path found using inspect.exe (Windows SDK) for Chrome 31.0.1650.63 m (currently the latest stable)  
  36.                     var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));  
  37.                     if (elm1 == null) { continue; } // not the right chrome.exe  
  38.                     // here, you can optionally check if Incognito is enabled:  
  39.                     //bool bIncognito = TreeWalker.RawViewWalker.GetFirstChild(TreeWalker.RawViewWalker.GetFirstChild(elm1)) != null;  
  40.                     var elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(  
  41.                     var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));  
  42.                     var elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));  
  43.                     elmUrlBar = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));  
  44.                 }  
  45.                 catch  
  46.                 {  
  47.                     // Chrome has probably changed something, and above walking needs to be modified. :(  
  48.                     // put an assertion here or something to make sure you don't miss it  
  49.                     continue;  
  50.                 }  
  51.   
  52.                 // make sure it's valid  
  53.                 if (elmUrlBar == null)  
  54.                 {  
  55.                     // it's not..  
  56.                     continue;  
  57.                 }  
  58.   
  59.                 // elmUrlBar is now the URL bar element. we have to make sure that it's out of keyboard focus if we want to get a valid URL  
  60.                 if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))  
  61.                 {  
  62.                     continue;  
  63.                 }  
  64.   
  65.                 // there might not be a valid pattern to use, so we have to make sure we have one  
  66.                 AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();  
  67.                 if (patterns.Length == 1)  
  68.                 {  
  69.                     string ret = "";  
  70.                     try  
  71.                     {  
  72.                         ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;  
  73.                     }  
  74.                     catch { }  
  75.                     if (ret != "")  
  76.                     {  
  77.                         // must match a domain name (and possibly "https://" in front)  
  78.                         if (Regex.IsMatch(ret, @"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))  
  79.                         {  
  80.                             // prepend http:// to the url, because Chrome hides it if it's not SSL  
  81.                             if (!ret.StartsWith("http"))  
  82.                             {  
  83.                                 ret = "http://" + ret;  
  84.                             }  
  85.                             Console.WriteLine("Open Chrome URL found: '" + ret + "'");  
  86.                         }  
  87.                     }  
  88.                     continue;  
  89.                 }  
  90.             }  
  91.   
  92. // THIS IS CODE FOUND ON THE INTERNET THAT I WANT TO TEST //  
  93. // END//  
  94.   
  95.         }  
  96.     }  
  97. }  
Thank you in advanced for all your replies!

Answers (2)