簡體   English   中英

將Cmd輸出保存到C#中的txt文件中

[英]Save The Cmd output into txt file in C#

我如何將CMD命令保存到C#中的txt文件中

或如何在C#中顯示命令提示符

這是我的代碼

                      private void button1_Click(object sender, EventArgs e)
    {
        var p = new Process();

        string path = @"C:\Users\Microsoft";
        string argu = "-na>somefile.bat";
        ProcessStartInfo process = new ProcessStartInfo("netstat", argu);
        process.RedirectStandardOutput = false;
        process.UseShellExecute = false;
        process.CreateNoWindow = false;

        Process.Start(process);

        p.StartInfo.WorkingDirectory = path;
        p.StartInfo.FileName = "sr.txt";
        p.Start();
        p.WaitForExit();
    }

您可以重定向標准輸出:

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
    //
    // Setup the process with the ProcessStartInfo class.
    //
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = @"C:\7za.exe"; // Specify exe name.
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    //
    // Start the process.
    //
    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        using (StreamReader reader = process.StandardOutput)
        {
        string result = reader.ReadToEnd();
        Console.Write(result);
        }
    }
    }
}

代碼來自這里

還要看這個答案: 將輸出重定向到文本文件C#

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM