Start hidden Internet Explorer

大城市里の小女人 提交于 2019-12-10 14:45:19

问题


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!


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/31402342/start-hidden-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!