2013-04-16 87 views
6

Tôi muốn chạy một cmd và chạy một số lệnh trong đó. Tôi đã viết mã này:chạy lệnh trong cmd bằng cách sử dụng C#

Process p = new Process(); 
ProcessStartInfo info =new ProcessStartInfo(); 

info.FileName = "cmd.exe"; 
info.WorkingDirectory = this.workingDirectory; 
info.RedirectStandardInput = true; 
info.UseShellExecute = false; 
info.CreateNoWindow = true; 
p.StartInfo = info; 

var x=p.Start(); 
using (StreamWriter sw = p.StandardInput) 
{ 
    if (sw.BaseStream.CanWrite) 
    { 
     sw.WriteLine(@"set path=c:\temp"+ ";%path%"); 
     sw.WriteLine(@"@MyLongproces.exe"); 
    } 
} 

Nhưng nó không hoạt động:

  1. Tôi không thể nhìn thấy cửa sổ lệnh (ngay cả khi tôi đặt info.CreateNoWindow để false).
  2. Lệnh của tôi không chạy.

Sự cố là gì? và làm thế nào tôi có thể sửa nó?

  • Update1

Mã này không làm việc:

string binDirectory = Path.Combine(FileSystem.ApplicationDirectory, this.binFolderName); 
    ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command)); 
    info.RedirectStandardInput = false; 
    info.RedirectStandardOutput = true; 
    info.UseShellExecute = false; 
    info.CreateNoWindow = false; 
    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
    proc.StartInfo = info; 
    proc.Start(); 
    string result = proc.StandardOutput.ReadToEnd(); 

Không có cửa sổ cmd được hiển thị và kết quả là nó "".

Nhưng mã này hoạt động:

 Process.Start(Path.Combine(binDirectory, command)); 

Vấn đề với mã trên là:

  1. tôi không thể xác định thư mục làm việc.
  2. Nó hiển thị cửa sổ CMD khi tôi không muốn nó hiển thị.

Bất kỳ ý tưởng nào tại sao nó không hoạt động?

+0

Bạn không có đối số, thêm một mặt hàng như @ "/ k"; – Derek

+0

Nếu bạn chỉ muốn mở một thư mục, bạn có thể bắt đầu một quá trình với đường dẫn thư mục, bạn không cần phải thực thi cmd để thực hiện nó. – Moondustt

Trả lời

0

Bạn vẫn cần nói cho quá trình của bạn biết bạn muốn chạy lệnh nào. Trong trường hợp này, có vẻ như bạn muốn nó để bắt đầu cmd.exe

1

Bạn đang thiết lập các tùy chọn CreateNoWindow:

info.CreateNoWindow = true; 

ProcessStartInfo.CreateNoWindow - đúng nếu quá trình này nên bắt đầu mà không tạo ra một cửa sổ mới để chứa nó; ngược lại, sai. Giá trị mặc định là sai.

0

Hãy thử rằng:

Trước

p.StartInfo = info; 

Chèn

info.Arguments = "/c ping www.google.com.br"; //command here 
0

Tại sao bạn viết những điều dificult, đây là cách để chạy các lệnh:

try { 

    System.Diagnostics.ProcessStartInfo procStartInfo = 
     new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); 

    StreamReader.procStartInfo.RedirectStandardOutput = true; 
    procStartInfo.UseShellExecute = false; 

    procStartInfo.CreateNoWindow = true; 

    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
    proc.StartInfo = procStartInfo; 
    proc.Start(); 

    string result = proc.StandardOutput.ReadToEnd(); 

    Console.WriteLine(result); 
    } 
    catch (Exception objException) 
    { 
     // Log the exception 
    } 
+0

Xin cảm ơn, hãy xem cập nhật cho câu hỏi. – mans

0

Tôi đoán bạn đang cố gắng chạy MyLongProcess.exe tại đây. Nếu có, không cần phải khởi động dấu nhắc lệnh.Bạn có thể thử như sau:

//Create process info and set arugments here and then assign it to process object 
// If the exe does not expect any arguments, simply use line below 
Process process = Process.Start("MyLongProcess.exe"); 
0

Bạn có thể thử

//Get the paths list and add your custom path to it 
string paths = System.Environment.GetEnvironmentVariable("PATH") + @";C:\temp"; 

//Create a array consisting of all the paths 
string[] pathArray = paths.Split(';'); 

//Search the paths for the first path in which your exe is present 
string exePath = pathArray.Select(x => System.IO.Path.Combine(x, "MyLongproces.exe")) 
          .Where(x => System.IO.File.Exists(x)) 
          .FirstOrDefault(); 

if (string.IsNullOrWhiteSpace(exePath) == false) 
{ 
    //start your exe 
    System.Diagnostics.Process.Start(exePath); 
} 
0

mặc dù điều này là khá cũ, theo ý kiến ​​của tôi là vấn đề nằm trong tham số quy định cho /c của cmd. Nếu bạn chỉ định đối số có dấu cách, bạn cần sử dụng ký tự " khi bắt đầu và kết thúc. Vì vậy, tôi khuyên bạn nên thay đổi điều này

ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command)); 

với điều này.

ProcessStartInfo info = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", Path.Combine(binDirectory, command))); 
0

cố gắng để thêm dòng này

info.Verb = "runas"; 
Các vấn đề liên quan