English 中文(简体)
启动命令窗口并在内部运行命令
原标题:Start command windows and run commands inside
I need to start the command window with some arguments and run more commands inside. For example, launch a test.cmd and run mkdir. I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process? How do I go about this? Unable to add comments to answer... SO writing here. Andrea, This is what I was looking for. However the above code doesnt work for me. I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands. psi.FileName = @"c: est.cmd"; psi.Arguments = @"arg0 arg1 arg2"; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.CreateNoWindow = true; psi.UseShellExecute = false; Process p = new Process(); p.StartInfo = psi; p.Start(); p.StandardInput.WriteLine(@"dir>c: esults.txt"); p.StandardInput.WriteLine(@"dir>c: esults2.txt");
最佳回答
You can send further commands to cmd.exe using the process standard input. You have to redirect it, in this way: var startInfo = new ProcessStartInfo { FileName = "cmd.exe", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; var process = new Process {StartInfo = startInfo}; process.Start(); process.StandardInput.WriteLine(@"dir>c: esults.txt"); process.StandardInput.WriteLine(@"dir>c: esults2.txt"); process.StandardInput.WriteLine("exit"); process.WaitForExit(); Remember to write "exit" as your last command, otherwise the cmd process doesn t terminate correctly...
问题回答
The /c parameter to cmd. ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause"); Process.Start(start); (pause is just an example of what you can run) But for creating a directory you can do that and most other file operations from c# directly System.IO.Directory.CreateDirectory(@"c:fooar"); Start a cmd from c# is useful only if you have some big bat-file that you don t want to replicate in c#.
What are you trying to achieve? Do you actually need to open a command window, or do you need to simply make a directory, for example? mkdir is a windows executable - you can start this program in the same way you start cmd - there s no need to start a command window process first. You could also create a batch file containing all the commands you want to run, then simply start it using the Process and ProcessStartInfo classes you re already using.
How come this doesn t work? var startInfo = new ProcessStartInfo { FileName = "cmd.exe", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = false }; var process = new Process { StartInfo = startInfo }; process.Start(); process.StandardInput.WriteLine(@" dir"); process.WaitForExit();
here is a code example of F#, that does not work at all for some reason, it is in F#: open System open System.Diagnostics let runProc filename args startDir : seq * seq = let timer = Stopwatch.StartNew() let procStartInfo = ProcessStartInfo( RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, FileName = filename, Arguments = args ) match startDir with | Some d -> procStartInfo.WorkingDirectory <- d | _ -> () let outputs = System.Collections.Generic.List() let errors = System.Collections.Generic.List() let outputHandler f (_sender:obj) (args:DataReceivedEventArgs) = f args.Data use p = new Process(StartInfo = procStartInfo) p.OutputDataReceived.AddHandler(DataReceivedEventHandler (outputHandler outputs.Add)) p.ErrorDataReceived.AddHandler(DataReceivedEventHandler (outputHandler errors.Add)) let started = try p.Start() with | ex -> ex.Data.Add("filename", filename) reraise() if not started then failwithf "Failed to start process %s" filename printfn "Started %s with pid %i" p.ProcessName p.Id p.BeginOutputReadLine() p.BeginErrorReadLine() p.WaitForExit() timer.Stop() printfn "Finished %s after %A milliseconds" filename timer.ElapsedMilliseconds let cleanOut l = l |> Seq.filter (fun o -> String.IsNullOrEmpty o |> not) cleanOut outputs,cleanOut errors i do not know what the problem is with this, but a function may not be called. that is all I am guessing the problem is.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签