2012-02-17 11 views
7

Chcę uruchomić okno dialogowe uruchamiania (Windows + R) z systemu Windows w moim kodzie C#.Jak uruchomić okno "uruchom" z C#

Zakładam, że można to zrobić za pomocą programu explorer.exe, ale nie jestem pewien jak.

+5

Czy istnieje powód chcesz okno „Uruchom” zamiast rozpoczęciem procesu bezpośrednio przez Process.Start? –

+11

Mam bardzo silne podejrzenie, że jest to pytanie "[Shoe or Bottle?] (Http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx)". –

+1

Rozumiem, że chcesz uruchomić polecenie "Uruchom" dostarczone przez Eksploratora Windows. Czy możesz wyjaśnić, dlaczego chcesz to zrobić? –

Odpowiedz

5

RunFileDlg API jest obsługiwane i mogą być usunięte przez Microsoft z przyszłymi wersjami Windows (Daję państwom członkowskim zobowiązanie do wstecznej kompatybilności oraz fakt, że ten API, choć nieudokumentowany, ap gruszki są dość powszechnie znane, co sprawia, że ​​jest to mało prawdopodobne, ale wciąż istnieje taka możliwość).

Obsługiwany sposób uruchomienia okna dialogowego uruchamiania jest oparty na metodzie IShellDispatch::FileRun.

W języku C# można uzyskać dostęp do tej metody, przechodząc do Dodaj odniesienie, wybierz kartę COM i wybierz "Sterowanie i kontrola Microsoft Shell". Po wykonaniu tej czynności można uruchomić okno następująco:

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

Tak, RunFileDlg API oferuje więcej możliwości dostosowywania, ale ma to tę zaletę, że jest udokumentowane, wspierany, a zatem mało prawdopodobne, aby przełamać w przyszłości.

+0

Idealne rozwiązanie. – BlueM

+0

Czy można przekazać argument (polecenie uruchomienia)? – xr280xr

9

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

Możliwe wartości 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). 

Zobacz także How to programmatically open Run c++?

+0

nice! dzięki :) – cyptus

+0

Zauważ, że technicznie jest to nieudokumentowane – Justin

+0

Czy to nieudokumentowane połączenie? Nie mogłem znaleźć żadnego odniesienia do tego na MSDN lub na http://pinvoke.net –

2

Inną metodą byłoby emulowanie kombinacji klawiszy 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); 
    } 
} 

i zadzwonić:

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