2013-04-16 18 views
6

Chcę uruchomić cmd i uruchomić niektóre polecenia w nim. Napisałem ten kod:uruchamianie poleceń w cmd przy użyciu 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"); 
    } 
} 

Ale to nie działa:

  1. Nie widzę okno poleceń (nawet kiedy ustawić info.CreateNoWindow do false).
  2. Moje polecenie nie działa.

Na czym polega problem? i jak mogę to naprawić?

  • Update1

Ten kod nie działa:

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(); 

Nie cmd okno jest pokazane i spowodować, że "".

Ale ten kod działa:

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

Problem z powyższego kodu jest:

  1. Nie mogę określić katalog roboczy.
  2. Pokazuje okno CMD, gdy nie chcę, aby się wyświetlało.

Każdy pomysł, dlaczego to nie działa?

+0

nie masz argumentu, dodasz jak @ "/ k"; – Derek

+0

Jeśli chcesz tylko otworzyć katalog, możesz rozpocząć proces ze ścieżką do katalogu, nie musisz wykonywać polecenia cmd, aby to zrobić. – Moondustt

Odpowiedz

0

Nadal musisz powiedzieć procesowi, jakie polecenie chcesz uruchomić. W tym przypadku, to brzmi jak chcesz go uruchomić cmd.exe

1

ustawiania opcji CreateNoWindow:

info.CreateNoWindow = true; 

ProcessStartInfo.CreateNoWindow - True jeśli proces ten powinien być zaczął bez tworzenia nowego okna zawierać to; w przeciwnym razie false. Ustawieniem domyślnym jest fałsz.

0

Spróbuj tego:

Przed

p.StartInfo = info; 

Insert

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

Dlaczego piszesz trudna rzeczy, oto jak uruchomić komendy:

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

Dzięki, zobacz aktualizację pytania. – mans

0

Zgaduję, że próbujesz uruchomić plik MyLongProcess.exe tutaj. Jeśli tak, nie ma potrzeby uruchamiania wiersza polecenia.Można spróbować wykonać następujące czynności:

//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

Państwo mogli spróbować tego

//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

althought to dość stary, moim zdaniem problem leży w granicach określonego parametru dla /c z cmd. Jeśli podasz argument zawierający spacje, musisz użyć znaku " na początku i na końcu. Więc sugerowałbym zmianę tego

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

do tego.

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

Spróbuj dodać ten wiersz

info.Verb = "runas"; 
Powiązane problemy