简体   繁体   中英

getting a blank cmd window by running command line from C#

I am trying to open command line in the c folder, from C#.

the expectation is to see this in the command line window:

C:>

but instead i am getting a blank cmd window.

this is the code:

var startInfo = new System.Diagnostics.ProcessStartInfo
{
    WorkingDirectory = @"c:\",
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
    FileName = "cmd.exe",
    RedirectStandardInput = true,
    UseShellExecute = false
 };
 Console.ReadKey();

WaitForExit is what you are looking for.

EDIT:

var startInfo = new System.Diagnostics.ProcessStartInfo
{
    WorkingDirectory = @"c:\",
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
    FileName = "cmd.exe",
    RedirectStandardInput = true,
    UseShellExecute = false
 };
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();

This will halt the execution of all statements after WaitForExit . The time you close the command window, the statements following WaitForExit will be executed.

i think you're looking for this first we'll create a process for CMD.exe and then passes "/K cd \\". "/K" will "CMD.exe" to receive parameter and stay open, while "cd \\" will take us to "C:/" which is your requirement

System.Diagnostics.Process.Start("CMD.exe", "/K \"cd /\"");
Console.ReadKey();

I believe if you use the /K Argument when executing the command, you should have a command prompt running at C:\\

        ProcessStartInfo startinfo = new ProcessStartInfo();
        startinfo.FileName = "cmd.exe";
        startinfo.WorkingDirectory = @"C:\";
        startinfo.Arguments = "/K";
        startinfo.UseShellExecute = false;
        Process.Start(startinfo);

Or

        Process command = new Process();
        command.StartInfo.UseShellExecute = false;
        command.StartInfo.WorkingDirectory = @"C:\";
        command.StartInfo.Arguments = "/K";
        command.StartInfo.FileName = "cmd.exe";
        command.Start();

The /K argument executes the cmd.exe command and keeps the window open :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM