简体   繁体   中英

C# command prompt output

I need to use a command prompt in my project. Everything is ok, but the output is not what I want. If I do this:

        ProcessStartInfo info = new ProcessStartInfo("cmd","/c dir c:\\test");
        info.RedirectStandardOutput = true;
        info.RedirectStandardInput = true;
        info.CreateNoWindow = true;
        info.UseShellExecute = false;
        Process p = new Process();
        p.StartInfo = info;
        p.Start();
        string iii = p.StandardOutput.ReadToEnd();
        textBox1.Text = iii;

The result is ok. Exactly as I want. But I also need to send some more commands. So I am doing this:

        ProcessStartInfo info = new ProcessStartInfo("cmd");
        info.RedirectStandardOutput = true;
        info.RedirectStandardInput = true;
        info.CreateNoWindow = true;
        info.UseShellExecute = false;
        Process p = new Process();
        p.StartInfo = info;
        p.Start();
        StreamWriter write = p.StandardInput;
        write.WriteLine("dir c:\\test");
        write.Close();
        string iii = p.StandardOutput.ReadToEnd();
        textBox1.Text = iii;

But the result is not like before. It's giving the path and everything in cmd, which I don't want. I need only the result from the command prompt and nothing else. Hope someone can help. Thanks for reading my problem.

You have shown an implementation for something you want to do, and posed the question in terms of "why does my implementation not do what I expect". If we can take a step back and let us know "what I need is ..." (WINI) then we can (I think) give you a real answer.

For example:

If I take the WINI as "I need a directory listing of 'c:\\test'" then I would say create a real simple little console app that gives you the raw data you want. The rest is formatting, think separation of concerns.

You then have a probe app that can give you whatever you like in a format best for you. You then use parser to parse the output.

So your little console app can do something like:

    public class Hyperthetical
{
    public void MyDemoCall(string root)
    {
        Console.WriteLine("-- start --");
        GiveMeADirectoryListingAsIWantIt(root)
        Console.WriteLine("-- end --");
    }

    public void GiveMeADirectoryListingAsIWantIt(string directory)
    {
        Console.WriteLine("Folder '{0}':", directory);

        foreach (var filePath in System.IO.Directory.GetFiles(directory))
        {
            var fileInfo = new FileInfo(filePath);
            Console.WriteLine("File: '{0}', {1}", fileInfo.Name, fileInfo.Length);
        }

        foreach (var subFolders in System.IO.Directory.GetDirectories(directory))
        {
            GiveMeADirectoryListingAsIWantIt(subFolders);
        }
    }
}

Not a direct answer, but just maybe the real answer.

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