question

andyaa-2217 avatar image
0 Votes"
andyaa-2217 asked andyaa-2217 answered

refresh Edge via c#

Hi,

we are migrating from IE to edge and one of our software was refreshing IE pages.
It was perfectly running for IE but of course not for Edge
could you provide me equivalent for edge of code here under?

thanks a lot

foreach (SHDocVw.InternetExplorer ieInst in new SHDocVw.ShellWindowsClass())
{
if (String.IsNullOrEmpty(ieInst.LocationURL) == true) continue;
foreach (string S in res)
{
if (ieInst.LocationURL.ToUpper().Contains(S)) //check if url contain a specific text
{
Library.Log(ieInst.LocationURL);
ieInst.Refresh();
break;
}
}
}

NB : this code was executed by a windows services

ms-edge
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YuZhou-MSFT avatar image
0 Votes"
YuZhou-MSFT answered YuZhou-MSFT edited

Hi anonymous useraa-2217

If you want to refresh pages in Edge, you can get Edge process first. After that, you can get all the tabs in Edge and set focus to tabs and refresh them by simulating pressing F5 using the System.Windows.Forms.SendKeys.SendWait method.

The sample code is like below:

 using System;
 using System.Diagnostics;
 using System.Windows.Forms;
 using System.Windows.Automation;
    
 namespace refreshEdge
 {
     class Program
     {
         static void Main(string[] args)
         {
             Process[] procsEdge = Process.GetProcessesByName("msedge");
             foreach (Process Edge in procsEdge)
             {
                 if (Edge.MainWindowHandle != IntPtr.Zero)
                 {
                     AutomationElement root = AutomationElement.FromHandle(Edge.MainWindowHandle);
                     var tabs = root.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                     var elmUrl = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));                 
                     foreach (AutomationElement tabitem in tabs)
                     {
                         if (elmUrl != null)
                         {
                             AutomationPattern[] patterns = elmUrl.GetSupportedPatterns();
                             if (patterns.Length > 0)
                             {
                                 ValuePattern val = (ValuePattern)elmUrl.GetCurrentPattern(patterns[0]);
                                 string url = val.Current.Value;
                                 Console.WriteLine(url.Contains("bing.com"));                                 
                             }
                         }
                         tabitem.SetFocus();
                         SendKeys.SendWait("{F5}");                   
                     }
                 }
             }
         }
     }
 }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Regards,
Yu Zhou

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

thanks for answer, but it still issues.

The original software was a Windows Services running .NET framework 4.5.2
I don't have the System.Windows.Automation library. How do you add it? is it a part of a nugget package?

I have retried with a console app .NET 4.7.2 and I have the same issue.

Thanks for helps

Andy

0 Votes 0 ·

Hi anonymous useraa-2217

You need to add reference to UIAutomationClient and UIAutomationTypes to use System.Windows.Automation.
Please refer to the screenshot below:

124119-image.png

0 Votes 0 ·
image.png (59.3 KiB)
andyaa-2217 avatar image
0 Votes"
andyaa-2217 answered YuZhou-MSFT edited

Ok, thanks, I found it

Now tab is well focusing (all tabs are well displayed one by one ) but the command SendKeys.SendWait("{F5}"); doesn't work, any idea?

Extra question, how to check if url of active tab contain a specific text ( eg : msdn.com )

Thanks

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi anonymous useraa-2217

At my side, SendKeys.SendWait("{F5}") works well, it will refresh the tabs. You can check the screenshot below.

If you want to get the urls and compare them, you can use AutomationElement.NameProperty, then use Contains() to compare url strings. I've updated the complete code in my answer above, please check it.

124469-5.gif

0 Votes 0 ·
5.gif (1004.6 KiB)
andyaa-2217 avatar image
0 Votes"
andyaa-2217 answered

Thanks YuZouh, it work !

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.