我已经尝试了几个星期来从一个提升的进程运行一个非提升的Web浏览器,我尝试了各种各样的东西,复制了资源管理器令牌,使用WinSafer Apis mentioned here和其他各种技术一切都失败了。最后,我决定使用Microsoft建议使用Task Scheduler来运行该应用程序。
我使用了Task Scheduler Managed Wrapper,起初我尝试运行explorer.exe并将url作为命令传递但是没有用,所以我创建了一个虚拟可执行文件,它将使用Process.Start启动该站点。 / p>
以下是我创建任务的方法:
public static void LaunchWin8BrowserThroughTaskScheduler(string sURL)
{
String RunAsUserExecPath = AppDomain.CurrentDomain.BaseDirectory + "URLLaunch.exe";
String Command = string.Format("-w \"{0}\"", sURL);
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "URL Launch";
td.Principal.LogonType = TaskLogonType.InteractiveToken;
TimeTrigger trigger = new TimeTrigger(DateTime.Now.AddSeconds(2))
{
Enabled = true,
EndBoundary = DateTime.Now.AddSeconds(10)
};
td.Triggers.Add(trigger);
td.Actions.Add(new ExecAction( RunAsUserExecPath, Command, null));
td.Settings.StartWhenAvailable = true;
//Delete the task after 30 secs
td.Settings.DeleteExpiredTaskAfter = new TimeSpan(0,0,0,30);
ts.RootFolder.RegisterTaskDefinition("URL Launch", td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
}
}
这是我的虚拟可执行文件的代码:
static void Main(string[] args)
{
if(args.Length<=1)
return;
string sCmd = args[0];
string sArg = args[1];
if(string.IsNullOrEmpty(sCmd)||string.IsNullOrEmpty(sArg))
return;
switch (sCmd)
{
case "-w":
{
Process prs = Process.Start(sArg);
}
break;
}
}
此方法正常,浏览器确实是非升级的,我可以通过检查Windows 8任务管理器中的Elevated列来确认。
这里唯一的细微差别是浏览器没有作为最顶层的窗口启动,它在后台运行,我认为它与通过任务调度程序运行的事实有关。
这引起了我的问题,尤其是现代UI浏览器,因为Windows在启动页面时不会切换到它们。我可以看到该页面已成功在Chrome中启动,例如,在Windows 8模式下运行,但它不会切换到浏览器这一事实只是违反了此解决方法的全部目的。
我考虑使用SetForegroundWindow
但遗憾地运行上述示例或通过explorer.exe运行的URL,Process.Start返回null
。
我想知道是否有人可以帮我解决这个问题并能够在前台运行浏览器。
问候
答案 0 :(得分:1)
我已经能够用一种非常简单的方法来解决这个问题。
只需将快捷方式文件写入TempFolder之类的地方,然后通过explorer.exe执行即可:
public static void GoURL(string url)
{
string sysPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
string ExplorerPath = Path.Combine(Directory.GetParent(sysPath).FullName,
"explorer.exe");
string TempDir = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string shortcutPath = Path.Combine(TempDir, "Mylink.url");
urlShortcutToTemp(url, shortcutPath);
System.Diagnostics.Process.Start(ExplorerPath, shortcutPath);
}
private static void urlShortcutToTemp(string linkUrl, string shortcutPath)
{
using (StreamWriter writer = new StreamWriter(shortcutPath))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=" + linkUrl);
writer.Flush();
}
}
可以使用lnk快捷方式将相同的解决方案应用于可执行文件。