Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start hidden Internet Explorer

my company is using Sharepoint and ADFS. For the use of WebDav however we need the users to get some tokens which they only get by opening the Internet Explorer and navigate to two sites. However they will lose the token every ~30 Minutes, so it has to be a recurring task.

So now my job is to:

  • Open 2 Websites with IE
  • Every 30 Minutes
  • Don't annoy the user

My current solution is "kinda" working but I am not really satisfied with it. I have only VSExpress so no Services.

I have a minimized max opacity visible false Windows Form. I have a GPO which copies an EXE file to the computer and then creates a timed job that starts it every 30 minutes after login. However it is not really working out, people still have trouble accessing webdav if they don't run the EXE manually. Also whenever the EXE is running the current application the user is working in loses focus which is kinda annoying when you are typing something and have to click back in. My current code is looking like this:

    private void Form1_Load(object sender, EventArgs e)
    {
        MainMethod();
    }
    private void MainMethod()
    {
        RegistryKey root = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\InternetExplorer.ApplicationMedium\CLSID", false);
        if (root!=null)
        { 
            opensite();
            Application.Exit();
        }
    }
    private void opensite()
    {
        try
        {
            SHDocVw.InternetExplorer _ie1 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
            SHDocVw.InternetExplorer _ie2 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
            _ie1.Visible = false;
            _ie2.Visible = false;
            _ie1.Navigate("SITENAME1.html");
            _ie2.Navigate("SITENAME2.html");
            System.Threading.Thread.Sleep(10000);
            _ie1.Quit();
            _ie2.Quit();
        }
        catch(Exception e)
        {
        }
    }

However, I feel there is a much more elegant way to do this. I heard the only way to open a hidden IE is via

(SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));

But with this I rely on the registry key which not all clients have.

Can you help me open the IE in a reliable way and maybe have some tipps on how I should set the recurring task to just start every 30 minutes (because I think it is not doing it correctly atm).

Thank you all in advance.

EDIT:

Thanks to https://stackoverflow.com/users/5065008/daniel-waghorn I now replaced the opensite bit with:

private void Form1_Load(object sender, EventArgs e)
    {
        MainMethod();
    }
    private void MainMethod()
    {
        openProc("SITE1.html");
        openProc("SITE2.html");
        Application.Exit();
    }

    private void openProc(string site)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        startInfo.FileName = ProgramFiles + @"\Internet Explorer\iexplore.exe";
        startInfo.Arguments = "" + site + "";
        startInfo.CreateNoWindow = true;
        startInfo.ErrorDialog = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(startInfo);
    }

Thanks again!

like image 780
Asharon Avatar asked Sep 27 '22 08:09

Asharon


2 Answers

You can use ProcessStartInfo to create a new instance of IE:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ""C:\Program Files\Internet Explorer\iexplore.exe"";
startInfo.Arguments = "" + url + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);

You could use Environment.SpecialFolder.ProgramFiles to get the user's Program Files directory path if you don't want to hard-code it.

I must point out that startInfo.WindowStyle will start Internet Explorer hidden although if at any point IE decides to alter that value for any reason it may show.

Ideally if you aren't tied to using Internet Explorer to get the tokens another alternative would be to use the above code but target cURL or something similar. With this it will run in the command line which you can guarantee not to show or steal focus with startInfo.CreateNoWindow.

like image 150
Daniel Waghorn Avatar answered Oct 13 '22 03:10

Daniel Waghorn


I think you'll find your answer in one of these links:

Handle IE To filling a form c#

Opening a Hidden Internet Explorer Window without it getting Focus?

like image 40
NickGames Avatar answered Oct 13 '22 04:10

NickGames