refresh Edge via c#

andy aa 21 Reputation points
2021-08-16T08:41:19.793+00:00

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

Microsoft Edge Microsoft Edge development
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-08-17T08:48:59.957+00:00

    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 additional answers

Sort by: Most helpful
  1. andy aa 21 Reputation points
    2021-08-18T11:11:44.877+00:00

    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


  2. andy aa 21 Reputation points
    2021-08-19T05:37:22.68+00:00

    Thanks YuZouh, it work !

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.