简体   繁体   English

启动隐藏的Internet Explorer

[英]Start hidden Internet Explorer

my company is using Sharepoint and ADFS. 我的公司正在使用Sharepoint和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. 但是,对于WebDav的使用,我们需要用户获得一些令牌,这些令牌只能通过打开Internet Explorer并导航到两个站点来获得。 However they will lose the token every ~30 Minutes, so it has to be a recurring task. 然而,他们将每隔约30分钟丢失令牌,因此它必须是一个经常性的任务。

So now my job is to: 所以现在我的工作是:

  • Open 2 Websites with IE 用IE打开2个网站
  • Every 30 Minutes 每30分钟一次
  • 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. 我只有VSExpress所以没有服务。

I have a minimized max opacity visible false Windows Form. 我有一个最小化的最大不透明度可见假Windows窗体。 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. 我有一个GPO,它将EXE文件复制到计算机,然后创建一个定时作业,在登录后每30分钟启动一次。 However it is not really working out, people still have trouble accessing webdav if they don't run the EXE manually. 然而,如果他们没有手动运行EXE,人们仍然无法访问webdav。 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: 此外,每当EXE运行当前应用程序时,用户正在失去焦点,当您输入内容并且必须再次单击时,这有点烦人。我当前的代码如下所示:

    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 我听说打开隐藏的IE的唯一方法是通过

(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). 你能帮我以可靠的方式打开IE吗?我可能会对如何将重复任务设置为每30分钟启动一次(因为我认为它没有正确地执行atm)。

Thank you all in advance. 谢谢大家。

EDIT: 编辑:

Thanks to https://stackoverflow.com/users/5065008/daniel-waghorn I now replaced the opensite bit with: 感谢https://stackoverflow.com/users/5065008/daniel-waghorn,我现在用以下内容替换了opensite位:

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! 再次感谢!

You can use ProcessStartInfo to create a new instance of IE: 您可以使用ProcessStartInfo创建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. 如果您不想对其进行硬编码,可以使用Environment.SpecialFolder.ProgramFiles来获取用户的Program Files目录路径。

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. 我必须指出startInfo.WindowStyle将启动Internet Explorer隐藏,但如果IE在任何时候决定改变该值,它可能会显示任何原因。

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. 理想情况下,如果您不依赖于使用Internet Explorer获取令牌,则另一种选择是使用上面的代码,但目标是cURL或类似的东西。 With this it will run in the command line which you can guarantee not to show or steal focus with startInfo.CreateNoWindow . 使用它,它将在命令行中运行,您可以保证不使用startInfo.CreateNoWindow显示或窃取焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM