简体   繁体   English

如何在C#中将控制台应用程序连续输出到文本文件

[英]How to continuously output console application to text file in C#

I am trying to open up a hidden console application with arguments, and basically log it's output into a file continuously until it is stopped. 我试图用参数打开一个隐藏的控制台应用程序,并且基本上将其输出连续记录到文件中,直到停止为止。

I have tried using a memory stream and writing it into the file, and it did seem to work for a bit. 我曾尝试使用内存流并将其写入文件,但它似乎确实起作用了一点。 Now I am trying to take advantage of the DataRecievedEvent so I can further process the output. 现在,我正在尝试利用DataRecievedEvent的优势,以便进一步处理输出。 Right now I am not getting any output. 现在我没有任何输出。

Here is how I am opening the console application: 这是我打开控制台应用程序的方式:

StreamWriter writer = new StreamWriter("tsharkfieldoutput.txt", true)

private void capturePackets(int device)
        {

           string path =
                string.Format("-i " + device +
                              "  -O SNMP -T fields -e snmp.value.oid -e snmp.VarBind -e snmp.variable_bindings -e snmp.value.octets -e snmp.name -R udp src " +
                              destPort);
           string tshark = @"C:\Program Files\Wireshark\tshark.exe";
           ProcessStartInfo ps = new ProcessStartInfo();
           ps.FileName = tshark;
           ps.CreateNoWindow = true;
           ps.WindowStyle = ProcessWindowStyle.Hidden;
           ps.UseShellExecute = false;
           ps.CreateNoWindow = true;
           ps.RedirectStandardOutput = true;
           ps.Arguments = path;
           Process process = new Process();
           process.StartInfo = ps;
           process.Start();
           process.OutputDataReceived += new DataReceivedEventHandler(tshark_OutputDataReceived);
            //Not using stream reader any more.
           //StreamReader myStreamReader = process.StandardOutput;
            writer.Write("Begin tshark output- " + DateTime.Now + " - " + Environment.NewLine);

        }

        private void tshark_OutputDataReceived(object sender, DataReceivedEventArgs arg)
        {
            string tsharkline = arg.Data; //arg.Data contains the output data from the process...        

                writer.Write(tsharkline);

        }

I think the issue is that your reference to the process is going out of scope at the end of function capturePackets. 我认为问题在于您对过程的引用超出了功能capturePackets的范围。 You will either need to give the process variable the same scope as the writer or wait in the capturePackets function until the process exits using the WaitForExit() method. 您将需要为流程变量赋予与writer相同的作用域,或者在capturePackets函数中等待,直到使用WaitForExit()方法退出流程为止。 Although the process that you create continues to run, when the reference to it goes out of scope (via the process variable), the events will stop being processed. 尽管您创建的过程继续运行,但是当对其的引用超出范围(通过过程变量)时,事件将停止处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在后台连续运行ac#console应用程序 - how to continuously run a c# console application in background 如何在文本文件中显示字符串? (C#控制台应用程序) - How to display a string within a text file? (C# Console Application) 如何将C#应用程序的文本输出写入文本文件 - How to write text output of a C# application to a text file C#:重定向控制台应用程序输出:如何刷新输出? - C# : Redirect console application output : How to flush the output? 在控制台应用程序中覆盖txt文件C#中的输出 - Overwrite output in txt file C# in console application 在 C# DLL 文件中作为进程运行时,如何获取控制台应用程序输出? - How can I obtain console application output when running it as a process in a C# DLL file? 输出/日志Console.Read&Console。从Console应用程序写入TXT / XML文件C# - Output/Log Console.Read & Console.Write from a Console application to a TXT/XML File C# 如何将连续控制台输出保存到c#中的文本文件? - How do I save continuous console output to a text file in c#? 如何在c#控制台应用程序中输出希腊符号? - How to output greek symbols in c# Console Application? 如何在具有Windows应用程序输出类型的C#程序上显示控制台 - How to show the console on a C# program with Windows application output type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM