2009-09-16 12 views
21

Come aprire una pagina Web in IE mentre si fa clic su un pulsante in un'applicazione C#. La mia intenzione è di creare un accesso Web per un'applicazione C# che deve essere aperto in IE, in larghezza e altezza specificate, e deve chiamare una funzione nel programma applicativo.aprire una pagina Web in IE utilizzando C#

+1

Una funzione come in una funzione JavaScript nell'applicazione Web? –

+0

L'unico modo in cui so che il codice C# interagisce con il DOM sul lato * client * consiste nell'integrare l'IE in WinForm come controller. Altrimenti, potresti scrivere il tuo componente aggiuntivo IE che lo farà per te Se tutto ciò che vuoi è chiamare una funzione JavaScript, puoi usare l'opzione che ho suggerito sotto (come risposta) e usare un parametro nell'URL per farlo –

+0

Se vuoi fare un'autenticazione remota di qualche tipo, vuoi sicuramente un servizio web? Quello che stai proponendo sembra un po 'insolito (non che io voglia scoraggiarti dall'investigare cose insolite). – Xiaofu

risposta

39

da http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    public class MyProcess 
    { 

     /// <summary> 
     /// Opens the Internet Explorer application. 
     /// </summary> 
     public void OpenApplication(string myFavoritesPath) 
     { 
      // Start Internet Explorer. Defaults to the home page. 
      Process.Start("IExplore.exe"); 

      // Display the contents of the favorites folder in the browser. 
      Process.Start(myFavoritesPath); 

     } 

     /// <summary> 
     /// Opens urls and .html documents using Internet Explorer. 
     /// </summary> 
     public void OpenWithArguments() 
     { 
      // url's are not considered documents. They can only be opened 
      // by passing them as arguments. 
      Process.Start("IExplore.exe", "www.northwindtraders.com"); 

      // Start a Web page using a browser associated with .html and .asp files. 
      Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); 
      Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); 
     } 

     /// <summary> 
     /// Uses the ProcessStartInfo class to start new processes, both in a minimized 
     /// mode. 
     /// </summary> 
     public void OpenWithStartInfo() 
     { 

      ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); 
      startInfo.WindowStyle = ProcessWindowStyle.Minimized; 

      Process.Start(startInfo); 

      startInfo.Arguments = "www.northwindtraders.com"; 

      Process.Start(startInfo); 

     } 

     public static void Main() 
     { 
        // Get the path that stores favorite links. 
        string myFavoritesPath = 
        Environment.GetFolderPath(Environment.SpecialFolder.Favorites); 

        MyProcess myProcess = new MyProcess(); 

      myProcess.OpenApplication(myFavoritesPath); 
      myProcess.OpenWithArguments(); 
      myProcess.OpenWithStartInfo(); 

       }  
    } 
} 
+0

grazie Tzury, ma ho aggiornato la domanda con tutte le mie esigenze, guarda su di esso – raki

+0

L'unico modo in cui so che il tuo codice C# interagisce con il DOM sul lato client consiste nell'integrare l'IE in WinForm come controller . Altrimenti, puoi scrivere il tuo componente aggiuntivo di IE che lo farà per te Se tutto ciò che vuoi è chiamare una funzione JavaScript, puoi usare l'opzione che ho suggerito sotto (come risposta) e usare un parametro nell'URL per farlo - Tzury 3 min fa [elimina questo commento] –

18
System.Diagnostics.Process.Start("iexplore", "http://example.com"); 
5

C'è un modo per aprire una pagina nel browser predefinito System.Diagnostics.Process.Start(url);

Se si desidera specificamente per aprirlo in IE si avrà probabilmente bisogno di creare un nuovo IE elaborare con URL come argomento.

UPD: (. Es http://stackoverflow.com/page?runFunction=1) Se si desidera eseguire una funzione, inserire i parametri di entrare nella vostra stringa URL e nel controllo codice dell'applicazione per il parametro runFunction e in base al suo valore di decidere se l'applicazione deve eseguire il funzione.

Non penso sia possibile specificare i nuovi valori di larghezza e altezza della finestra di IE, potrebbe essere necessario utilizzare javascript per questo.

+1

QUESTA È LA RISPOSTA GIUSTA: System.Diagnostics.Process.Start (url), utilizza il browser predefinito ed evita molti altri fattori relativi a sistemi, installazioni e personalizzazioni. –

Problemi correlati