Sudani
Sudani

Reputation: 73

how to allow only one instance of Internet explorer at any time?

I have used this method i found online, which will take a Uri then check if the uri is opened in an IE browser if it is opened it will prevent the opening of new tab or instance of the same site, if not a new IE tab will open which is great for what i was looking for the method is bellow:

public static void LaunchSite(Uri sitUrl)
{

    SHDocVw.ShellWindows w = new SHDocVw.ShellWindows();
    bool found = false;
    foreach (SHDocVw.ShellBrowserWindow item in w)
    {
        var doc = item.LocationURL;
        if (!string.IsNullOrEmpty(doc))
            if (doc == sitUrl.AbsoluteUri)
            {
                found = true;
                break;
            }
    }
    if (!found)
        Process.Start(sitUrl.AbsoluteUri);
}

Then used the method in a button_click event handler as follow:

 private void btnSubs_Click(object sender, RoutedEventArgs e)
{

    Uri oxfArt = new Uri(@"http://www.somesite.com/subscriber/");
    StartProcess.LaunchSite(oxfArt);

}

my questions are :

  1. within the method how can i check if there is already an instance of IE open, if it is i need to open the site within that IE window not as a new tab or new IE instance, basically i need to be able to have one instance and one windows of all my sites to be opened in current window?.
    1. within the button event handler i need to use switch statement that uses different URI to save me creating different button event handler for each URI, How would I be able to achieve that

Upvotes: 0

Views: 748

Answers (2)

Sudani
Sudani

Reputation: 73

The answer to my 2nd question has 2 option for solution:

 Button btnSub = e.Source as Button;
        if (btnSub != null)
        {
            Uri uri = new Uri((string)btnSub.Tag);
            StartProcess.LaunchSite(uri);
        }

within the butonn click event handler. then in the button triggered the event we need to set the tag to the desired Uri as follow:

 <Button x:Name="theorytst"  Cursor="Hand"  Tag="http://theorytestpro.co.uk/">

the 2nd solution is to use the switch statement using the button x:Name and the button tag property as before then cast button tag as Uri to pass to the calling method StartProcess.LaunchSite(uri);

switch (btnSub.Name)
        {

            case "oxfArt":
                Uri uri = new Uri((string)btnSub.Tag);
                StartProcess.LaunchSite(uri);
                break;
            case "theorytst":
                Uri uri1 = new Uri((string)btnSub.Tag);
                StartProcess.LaunchSite(uri1);
                break;
            default:
                break;
        }

Upvotes: 1

StepUp
StepUp

Reputation: 38114

  1. I need to be able to have one instance and one windows of all my sites to be opened in current window?

    Process[] pname = Process.GetProcessesByName("iexplore");
    if (pname.Length == 0)
    {
       Process.Start("IEXPLORE.EXE", "http://www.somesite.com/subscriber/");        
    }
    else
    {
       try
       {
            foreach (Process proc in Process.GetProcessesByName("iexplore"))
            {
                proc.Kill();
            }
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
    }
    
    1. Within the button event handler i need to use switch statement that uses different URI to save me creating different button event handler for each URI, How would I be able to achieve that

As I understand correctly you want to call various button handlers. You can do it using switch statement. For example:

string input="http://getyourURI.com"; //you should set your own http address.  
                                      //It is just an example
Uri uri = new Uri(input);    
string address = uri.OriginalString;
switch(address)
{
    case "hey.com":
        btnExcel_Click(null, null);
        break;
    case "another.com":
        callAnotherBtn_Click(null, null);
        break;
}

Upvotes: 0

Related Questions