2008-10-27 16 views

Odpowiedz

50
System.Diagnostics.Process.Start("PathToExe.exe"); 
+0

Co jeśli nie znam pełnej nazwy exe, chcę zadzwonić "PathTo * .exe" Czy to możliwe? – vishal

196

Oto fragment pomocny kodu:

using System.Diagnostics; 

// Prepare the process to run 
ProcessStartInfo start = new ProcessStartInfo(); 
// Enter in the command line arguments, everything you would enter after the executable name itself 
start.Arguments = arguments; 
// Enter the executable to run, including the complete path 
start.FileName = ExeName; 
// Do you want to show a console window? 
start.WindowStyle = ProcessWindowStyle.Hidden; 
start.CreateNoWindow = true; 
int exitCode; 


// Run the external process & wait for it to finish 
using (Process proc = Process.Start(start)) 
{ 
    proc.WaitForExit(); 

    // Retrieve the app's exit code 
    exitCode = proc.ExitCode; 
} 

Jest o wiele więcej można zrobić z tych obiektów, należy zapoznać się z dokumentacją: ProcessStartInfo, Process.

+6

Chciałem tylko wskazać, że to też wydaje się działać z innymi typami plików niż .exes. Po prostu wskaż plik, który chcesz otworzyć, a system Windows zrobi wszystko, aby go otworzyć: System.Diagnostics.Process.Start (@ "C: \ Users \ Blank \ Desktop \ PdfFile.pdf"); – DLeh

+0

WindowStyle = ProcessWindowStyle.Hidden jest dla nie-GUI.Po raz pierwszy uruchomiłem to nie powiodło się bez UseShellExecute = false, ale teraz działa. Nie jestem pewien, co się tam dzieje ... – Barton

+0

Prawdę mówiąc, nigdy nie próbowałem tego z aplikacją GUI. – sfuqua

18
System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe"); 
13

Jeśli masz problemy z używaniem System.Diagnostics jak miałem, należy użyć następującego prostego kodu, który będzie działał bez niego:

Process notePad = new Process(); 
notePad.StartInfo.FileName = "notepad.exe"; 
notePad.StartInfo.Arguments = "mytextfile.txt"; 
notePad.Start(); 
+5

Jak to jest "bez System.Diagonostics"? "Proces" znajduje się w System.Diagnostics. –

0

Używaj Process.Start, aby rozpocząć proces.

using System.Diagnostics; 
class Program 
{ 
    static void Main() 
    { 
    // 
    // your code 
    // 
    Process.Start("C:\\process.exe"); 
    } 
} 
0

Wystarczy umieścić swój file.exe w folderze \ Debug \ bin i użyć:

Process.Start("File.exe"); 
+0

Jak poprawia się twoja odpowiedź na wszystkie poprzednie? – mustaccio

1

Spróbuj tego:

Process.Start("Location Of File.exe"); 

(Upewnij się, że korzystać z biblioteki System.Diagnostics)

-1

Adame Kane

System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe"); 

to działało świetnie !!!!!

Powiązane problemy