2012-12-24 39 views
12

này hoạt động:Tại sao Process.Start ("cmd.exe", quy trình); không làm việc?

Process.Start("control", "/name Microsoft.DevicesAndPrinters"); 

Nhưng điều này không: (Nó chỉ mở ra một cửa sổ lệnh.)

ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); 
info.Arguments = "control /name Microsoft.DevicesAndPrinters"; 
Process.Start(info); 

Tại sao?

(Vâng, tôi biết họ không giống nhau. Nhưng điều thứ hai "nên" làm việc.)

+0

Nó hoạt động tốt cho tôi. Lỗi của bạn là gì? –

+2

@ SonerGönül Không có lỗi. Như tôi đã viết - nó chỉ mở ra một dấu nhắc lệnh. Nó sẽ mở DevicesAndPrinters. (Bạn có nghĩa là nó _that_ cho bạn?) – ispiro

Trả lời

29

Điều này là do cmd.exe hy vọng một switch /K để thực hiện một quá trình được chuyển thành một đối số. Hãy thử mã bên dưới

ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); 
info.Arguments = "/K control /name Microsoft.DevicesAndPrinters"; 
Process.Start(info); 

EDIT: Thay đổi thành /K ở trên. Bạn có thể sử dụng công tắc /C nếu bạn muốn cmd.exe đóng sau khi nó đã chạy lệnh.

1

Hãy thử điều này một

ProcessStartInfo info = new ProcessStartInfo("control"); 
info.Arguments = "/name Microsoft.DevicesAndPrinters"; 
Process.Start(info); 
+0

Tôi biết. Nhưng tôi đặc biệt muốn xem dòng lệnh _and_ kết quả. – ispiro

6

Bạn cần một chuyển đổi /c hoặc /k (tùy chọn cho cmd.exe) để lệnh được thực hiện. Hãy thử:

ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); 
info.Arguments = "/c control /name Microsoft.DevicesAndPrinters"; 
Process.Start(info); 
Các vấn đề liên quan