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:
- Tôi không thể nhìn thấy cửa sổ lệnh (ngay cả khi tôi đặt
info.CreateNoWindow
đểfalse
). - 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à:
- tôi không thể xác định thư mục làm việc.
- 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?
Bạn không có đối số, thêm một mặt hàng như @ "/ k"; – Derek
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