如何将控制台应用程序文本写入txt

时间:2018-09-21 20:29:53

标签: c# console console-application dump streamwriter

我正在制作一个使用procdump转储进程,然后使用Strings v2.53将转储转换为实际字符串的工具,在转换转储之后,我尝试将控制台应用程序中显示的所有字符串写入。文本文件。

我不知道我做错了什么,我在每个论坛上都尝试过ive,看看如何做,但我做不到。

这是我的代码

        string path2 = @"C:\Void\Dump\Dump.txt";


        Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "cmd.exe";
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;

        p.StartInfo = info;
        p.Start();
        StreamWriter sw = p.StandardInput;
        using (sw)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine(@"cd C:\Void\Dump");
                sw.WriteLine(@"strings -s");

                System.IO.StreamWriter stream = new System.IO.StreamWriter(path2);

                sw.AutoFlush = true;

                Console.SetOut(stream);

            }
        }

1 个答案:

答案 0 :(得分:1)

我将对此进行构建,以便您在写入控制台输出时写入文本文件。

File.WriteAllText(fileDirectoryPath, textData);//is the simplest way you can write to a file

在发送到WriteAllText的textData参数之前,还需要使用streamreader将流转换为文本。请参见下面的示例:

StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
File.WriteAllText("pathwayToDirectory", text);

这是集成到您的解决方案中的代码。请注意,我假设您将文本数据放入转储文件中:

   string path2 = @"C:\Void\Dump\Dump.txt";


    Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();
    StreamWriter sw = p.StandardInput;
    using (sw)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine(@"cd C:\Void\Dump");
            sw.WriteLine(@"strings -s");

            System.IO.StreamWriter stream = new System.IO.StreamWriter(path2);

              StreamReader reader = new StreamReader(path2);//Fixed this to have the right value
            string text = reader.ReadToEnd();//convert stream to text
            File.WriteAllText(path2, text);

            Console.SetOut(stream);
            sw.AutoFlush = true;

        }
    }