【问题标题】:How to write a console application text into a txt如何将控制台应用程序文本写入 txt
【发布时间】:2018-09-21 20:29:52
【问题描述】:

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

我不知道我做错了什么,我尝试了每个论坛帖子,我看到了如何做到这一点,但我就是做不到。

这是我的代码

        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);

            }
        }

【问题讨论】:

  • 我只需要知道如何将Console App正在写入的文本转换为txt。
  • 你的最终目标是什么?你想用txt文件做什么?如果您想再次在程序中读回该 txt,您也可以直接将 strings.exe 作为进程启动,而无需通过 cmd.exe 绕道。如果这样做,您可以只重定向此进程的 StandardOutput,并在您的 C# 程序中完全以编程方式读取 strings.exe 的输出,而无需临时 txt 文件...
  • 我制作了一个工具,它将自动创建 javaw 进程的转储,使用 strings.exe 将转储转换为可读字符串,然后使用 consoleapp 字符串在新的 txt 中搜索与作弊相关的字符串。因为我为之工作的人不希望那里的网络上有骗子,所以他们希望我制作一个工具来防止这种情况。我已经有了制作转储和转换它的过程。我只需要知道如何将所有显示的 ConsoleApp 文本转换为我可以搜索的 txt。
  • 所以本质上你确实希望在你的程序中搜索 strings.exe 的输出,还是我误解了你?
  • 是的,你完全正确。

标签: c# console console-application dump streamwriter


【解决方案1】:

我会构建它,以便您在写入控制台输出时写入文本文件。

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

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

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;

        }
    }

【讨论】:

  • 在您尝试声明“读者”prntscr.com/kx8lem 时出现错误,但要更详细地了解我正在尝试做什么。控制台应用程序将转储转换为字符串后,字符串在控制台应用程序中可见。所以我正在尝试创建一个全新的 txt,其中包含控制台应用程序中的所有文本。
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多