2012-02-17 24 views
7

Voglio avviare la finestra di dialogo Esegui (Windows + R) da Windows nel mio codice C#.Come avviare la finestra di dialogo "Esegui" da C#

Suppongo che questo può essere fatto utilizzando explorer.exe ma non sono sicuro di come.

+5

C'è una ragione si desidera che il dialogo "Esegui", invece di iniziare il processo direttamente tramite Process.Start? –

+11

Ho un forte sospetto che questa sia una domanda "[Scarpa o bottiglia?] (Http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx)". –

+1

Capisco che vuoi attivare il comando "Esegui" fornito da Windows Explorer. Puoi spiegare perché vuoi farlo? –

risposta

5

Il RunFileDlg API non è supportato e può essere rimosso da Microsoft da versioni future di Windows (Concederò l'impegno di MS nei confronti della retrocompatibilità e il fatto che questa API, sebbene priva di documenti, ap le pere abbastanza conosciute lo rendono improbabile, ma è comunque possibile).

Il metodo supportato per avviare la finestra di dialogo Esegui utilizza il metodo IShellDispatch::FileRun.

In C#, è possibile accedere a questo metodo andando su Aggiungi riferimento, selezionare la scheda COM e selezionare "Microsoft Shell Controls and Automation". Dopo aver fatto questo è possibile avviare la finestra di dialogo come segue:

Shell32.Shell shell = new Shell32.Shell(); 
shell.FileRun(); 

Sì, l'API RunFileDlg offre più possibilità di personalizzazione, ma questo ha il vantaggio di essere documentato, supportato, e quindi improbabile per rompere in futuro.

+0

Soluzione perfetta. – BlueM

+0

È possibile passare un argomento (il comando da eseguire)? – xr280xr

9

Uso RunFileDlg:

[DllImport("shell32.dll", EntryPoint = "#61", CharSet = CharSet.Unicode)] 
public static extern int RunFileDlg(
    [In] IntPtr hWnd, 
    [In] IntPtr icon, 
    [In] string path, 
    [In] string title, 
    [In] string prompt, 
    [In] uint flags); 

private static void Main(string[] args) 
{ 
    // You might also want to add title, window handle...etc. 
    RunFileDlg(IntPtr.Zero, IntPtr.Zero, null, null, null, 0); 
} 

I valori possibili per flags:

RFF_NOBROWSE = 1; //Removes the browse button. 
RFF_NODEFAULT = 2; // No default item selected. 
RFF_CALCDIRECTORY = 4; // Calculates the working directory from the file name. 
RFF_NOLABEL = 8; // Removes the edit box label. 
RFF_NOSEPARATEMEM = 14; // Removes the Separate Memory Space check box (Windows NT only). 

Vedi anche How to programmatically open Run c++?

+0

bello! grazie :) – cyptus

+0

Nota che tecnicamente questo non è documentato – Justin

+0

È una chiamata non documentata? Non ho trovato alcun riferimento a questo su MSDN o su http://pinvoke.net –

2

Un altro metodo sarebbe quello di emulare la combinazione di tasti Windows + R.

using System.Runtime.InteropServices; 
using System.Windows.Forms; 

static class KeyboardSend 
{ 
    [DllImport("user32.dll")] 
    private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); 

    private const int KEYEVENTF_EXTENDEDKEY = 1; 
    private const int KEYEVENTF_KEYUP = 2; 

    public static void KeyDown(Keys vKey) 
    { 
     keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0); 
    } 

    public static void KeyUp(Keys vKey) 
    { 
     keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); 
    } 
} 

e chiamare:

KeyboardSend.KeyDown(Keys.LWin); 
KeyboardSend.KeyDown(Keys.R); 
KeyboardSend.KeyUp(Keys.R); 
KeyboardSend.KeyUp(Keys.LWin); 
Problemi correlati