启动隐藏的Internet Explorer [英] Start hidden Internet Explorer

查看:286
本文介绍了启动隐藏的Internet Explorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司正在使用Sharepoint和ADFS.但是,对于WebDav的使用,我们需要用户获得一些令牌,这些令牌只能通过打开Internet Explorer并导航到两个站点来获得.但是,他们每隔30分钟就会丢失令牌,因此它必须是重复执行的任务.

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.

所以现在我的工作是:

  • 使用IE打开2个网站
  • 每30分钟
  • 不要惹恼用户

我当前的解决方案是"kinda"工作,但是我对此并不满意. 我只有VSExpress,所以没有服务.

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

我有一个最小化的最大不透明可见的错误Windows窗体. 我有一个GPO,它将一个EXE文件复制到计算机,然后创建一个定时作业,该作业在登录后每30分钟启动一次.但是,它还没有真正解决,如果人们不手动运行EXE,则仍然很难访问webdav.同样,每当EXE运行当前应用程序时,正在工作的用户就会失去焦点,这在您键入内容并必须单击回去时有点烦人. 我当前的代码如下:

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)
        {
        }
    }

但是,我觉得有一种更优雅的方法可以做到这一点.我听说打开隐藏的IE的唯一方法是通过

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.

您能帮我以一种可靠的方式打开IE,也许可以给我一些提示,以设置应如何将重复执行的任务设置为每30分钟开始一次(因为我认为它在atm上执行不正确).

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).

谢谢大家.

感谢 https://stackoverflow.com/users/5065008/daniel-waghorn 我现在将开放站点替换为:

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);
    }

再次感谢!

推荐答案

您可以使用ProcessStartInfo创建IE的新实例:

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);

如果您不想对其进行硬编码,则可以使用Environment.SpecialFolder.ProgramFiles来获取用户的Program Files目录路径.

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

我必须指出,startInfo.WindowStyle将隐藏Internet Explorer,尽管IE出于任何原因决定出于任何原因决定更改该值.

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.

理想情况下,如果您不希望使用Internet Explorer来获取令牌,另一种选择是使用上面的代码,但定位 cURL 或类似的内容.有了它,它将在命令行中运行,您可以保证不会使用startInfo.CreateNoWindow显示或窃取焦点.

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.

这篇关于启动隐藏的Internet Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆