IE浏览器打开chorme浏览器,如何打开其他浏览器
看到这个标题是否感觉奇怪,为什么要用IE浏览器打开chorme或者火狐浏览器等,这个功能从开发者来说不是一个好的需求,但确实是真实存在的,有用公司的背景历史比较复杂,而且公司有过长期的开发历史,这导致了公司有许多各种功能的系统,而且有用开发时间、语言、版本兼容性等问题的存在,所以有一个总的汇总页面,页面上放了各大系统的图标和链接,但是各系统各自自能在特点的浏览器或版本上跑。所以有这个妥协下的需求(我的内心是拒绝的)。
拿到这个需求,分析上来说浏览器本身并不具备打开其他浏览器的功能,所以只能借助其他方式,我们知道在winform中存在一个System.Diagnostics.Process类,使用它的Start()方法可以打开各种应用程序,由Start()所带的5各重载方法可以使用各种功能,所以我们可以从中选择自己需要的方法来使用。
代码如下
1 string mathonPath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"; 2 System.Diagnostics.Process p = new System.Diagnostics.Process(); //设定程序名 3 p.StartInfo.FileName = mathonPath; 4 p.StartInfo.Arguments = @"https://www.baidu.com/"; 5 p.Start();
Start()可以打开其他应用,那么我们要解决的问题就是做一个winform或者windows服务并让我们的IE和它们之间相互通讯,在IE上所幸我们的版本可以使用websocket,那就拿来借鉴引用一下。示例代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title></title> 6 <meta charset="utf-8" /> 7 <script> 8 var socket; 9 window.onload = function () { 10 connectSocketServer(); 11 } 12 function connectSocketServer() { 13 // 创建一个Socket实例 14 socket = new WebSocket('ws://localhost:10245'); 15 16 // 打开Socket 17 socket.onopen = function (event) { 18 19 // 发送一个初始化消息 20 socket.send('I am the client and I\'m listening!'); 21 22 // 监听消息 23 socket.onmessage = function (event) { 24 console.log('Client received a message', event); 25 }; 26 27 // 监听Socket的关闭 28 socket.onclose = function (event) { 29 console.log('Client notified socket has closed', event); 30 }; 31 32 // 关闭Socket.... 33 //socket.close() 34 }; 35 } 36 function sendMessage() { 37 if (socket) { 38 var messageBox = document.getElementById('messageBox'); 39 socket.send(messageBox.value); 40 messageBox.value = ""; 41 } else { 42 alert("连接断开"); 43 } 44 } 45 </script> 46 </head> 47 <body> 48 <ul> 49 <li><textarea></textarea></li> 50 <li><textarea id="messageBox"></textarea></li> 51 <li><button id="sendbtn" onclick="sendMessage()">SendMSG</button></li> 52 </ul> 53 </body> 54 </html>
在winform或windows服务端,我们使用superwebsocket做接收端使用。示例代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 WebSocketServer server; 2 private void startBtn_Click(object sender, EventArgs e) 3 { 4 var ip = "127.0.0.1"; 5 var port = "10245"; 6 //WebSocket服务器端启动 7 server = new WebSocketServer(); 8 if (!server.Setup(ip, int.Parse(port))) 9 { 10 Debug.Write("WebSocket服务器端启动失败"); 11 //处理启动失败消息 12 return; 13 } 14 15 //新的会话连接时 16 server.NewSessionConnected += server_NewSessionConnected; 17 //会话关闭 18 server.SessionClosed += server_SessionClosed; 19 //接收到新的消息时 20 server.NewMessageReceived += server_NewMessageReceived; 21 22 if (!server.Start()) 23 { 24 Debug.Write(string.Format("开启WebSocket服务侦听失败:{0}:{1}", server.Config.Ip, server.Config.Port)); 25 //处理监听失败消息 26 return; 27 } 28 Debug.Write("WebSocket服务器端启动成功"); 29 } 30 string KSessionId; 31 string VSessionId; 32 Dictionary<string, List<string>> msgDictionary = new Dictionary<string, List<string>>(); 33 private void server_NewMessageReceived(WebSocketSession session, string value) 34 { 35 Debug.WriteLine("接收到新的消息:{0} 来自:{1} 时间:{2:HH:MM:ss}", value, session.RemoteEndPoint, DateTime.Now); 36 if (value.StartsWith("K")) 37 { 38 KSessionId = session.SessionID; 39 //页面已链接 40 if (!String.IsNullOrEmpty(VSessionId)) 41 SendMsgToRemotePoint(VSessionId, string.Format("发来消息:{0}", value)); 42 //页面未链接 43 else 44 { 45 AddMsgToSessionId(VSessionId); 46 } 47 } 48 49 } 50 51 /// <summary> 52 /// 添加会话消息 53 /// </summary> 54 /// <param name="value"></param> 55 private void AddMsgToSessionId(string value) 56 { 57 if (value != null) 58 { 59 //消息列表包含页面会话ID 60 if (msgDictionary.ContainsKey(value)) 61 { 62 msgDictionary[value].Add(value); 63 } 64 //消息列表不包含页面会话ID 65 else 66 msgDictionary.Add(value, new List<string>() { value }); 67 } 68 } 69 70 /// <summary> 71 /// 会话关闭 72 /// </summary> 73 /// <param name="session"></param> 74 /// <param name="value"></param> 75 private void server_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) 76 { 77 Debug.WriteLine("会话关闭,关闭原因:{0} 来自:{1} 时间:{2:HH:MM:ss}", value, session.RemoteEndPoint, DateTime.Now); 78 if (session.SessionID == KSessionId) 79 SendMsgToRemotePoint(VSessionId, "已断开"); 80 else if (session.SessionID == VSessionId) 81 SendMsgToRemotePoint(KSessionId, "已断开"); 82 } 83 84 /// <summary> 85 /// 新的会话链接 86 /// </summary> 87 /// <param name="session"></param> 88 private void server_NewSessionConnected(WebSocketSession session) 89 { 90 Debug.WriteLine("新的会话连接 来自:{0} SessionID:{1} 时间:{2:HH:MM:ss}", session.RemoteEndPoint, session.SessionID, DateTime.Now); 91 if (msgDictionary.ContainsKey(session.SessionID)) 92 msgDictionary[session.SessionID].ForEach(item => session.Send(item)); 93 } 94 95 /// <summary> 96 /// 发送消息到 97 /// </summary> 98 /// <param name="sessionId"></param> 99 /// <param name="msg"></param> 100 private void SendMsgToRemotePoint(string sessionId, string msg) 101 { 102 var allSession = server.GetAppSessionByID(sessionId); 103 if (allSession != null) 104 allSession.Send(msg); 105 }
查看上面代码,修改下自己的规则,并优化下路径问题即可拿来使用,在项目实施的时候更改下浏览器的路径即可。
System.Diagnostics.Process还有许多其他的妙用,如另外一个需求:启动一个自己开发的程序后打开QQ,这时就需要你同样使用这个方式。