如何将连续控制台输出保存到c#中的文本文件?

时间:2015-06-24 12:39:11

标签: c#

我在节目制作方面相当不错,现在我已经被困在这一段时间了。我使用以下代码将连续数据输出流式传输到命令提示符。如何在手动关闭提示后确保将输出复制到文本文件中?

public static void Main(string[] args)
{
    Connector connector;
    Console.WriteLine("HelloEEG!");

    // Initialize a new Connector and add event handlers
    connector = new Connector();
    connector.DeviceConnected += new EventHandler(OnDeviceConnected);
    connector.DeviceConnectFail += new EventHandler(OnDeviceFail);
    connector.DeviceValidating += new EventHandler(OnDeviceValidating);

    // Scan for devices across COM ports
    // The COM port named will be the first COM port that is checked.
    connector.ConnectScan("COM40");

    // Blink detection needs to be manually turned on
    connector.setBlinkDetectionEnabled(true);

    Thread.Sleep(400000);

    System.Console.WriteLine("Goodbye.");
    connector.Close();
    Environment.Exit(0);
}

// Called when a device is connected 
static void OnDeviceConnected(object sender, EventArgs e)
{
    Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;

    Console.WriteLine("Device found on: " + de.Device.PortName);

    de.Device.DataReceived += new EventHandler(OnDataReceived);
}

// Called when scanning fails

static void OnDeviceFail(object sender, EventArgs e)
{
    Console.WriteLine("No devices found! :(");
}

// Called when each port is being validated
static void OnDeviceValidating(object sender, EventArgs e)
{
    Console.WriteLine("Validating: ");
}

// Called when data is received from a device
static void OnDataReceived(object sender, EventArgs e)
{
    Device.DataEventArgs de = (Device.DataEventArgs)e;
    DataRow[] tempDataRowArray = de.DataRowArray;

    TGParser tgParser = new TGParser();
    tgParser.Read(de.DataRowArray);

    /* Loops through the newly parsed data of the connected headset*/
    // The comments below indicate and can be used to print out the different data outputs. 
    for (int i = 0; i < tgParser.ParsedData.Length; i++)
    {
        //string temp = tgParser.ParsedData[1].ToString;
        //Console.WriteLine(tgParser.ParsedData.Length + " + " + temp);

        if (tgParser.ParsedData[i].ContainsKey("Raw"))
        {
            //Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
            //Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
        }

        if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
        {
            //The following line prints the Time associated with the parsed data
            //Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);
            Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);

            //A Poor Signal value of 0 indicates that your headset is fitting properly
            Console.WriteLine("Poor Signal:" + tgParser.ParsedData[i]["PoorSignal"]);

            poorSig = (byte)tgParser.ParsedData[i]["PoorSignal"];
        }

        if (tgParser.ParsedData[i].ContainsKey("Attention"))
        {
            //Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
            Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
        }

        if (tgParser.ParsedData[i].ContainsKey("Meditation"))
        {
            //Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
            Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
        }

        if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
        {
            //Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
            Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
        }

        if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
        {
            //Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
            Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

将每个控制台输出记录到文件会好得多。在手动关闭应用程序时,而不是等待写入文件。为了节省大量编码,您可以使用log4net来处理日志记录。

答案 1 :(得分:0)

有几种不同的方式可以解决这个问题,通过一些研究我相信你可以找到一些,但这是我将用于这个特定行动的解决方案:

正如琼西在评论中提到的那样,我首先会整理你的主要内容。创建一个单独的类来同时执行控制台writeline和文本输出。

在这个类中,可能会使用循环将数据输出到文件中,因此当手动关闭控制台时,您不必编写逻辑代码,这反过来会覆盖意外错误和丢失日志。

答案 2 :(得分:0)

这可能有用。

public static void WriteToFileAndConsole()
{
    string outFile = "ConsoleOut.txt";
    using (FileStream fileStream = new FileStream(outFile, FileMode.OpenOrCreate))
    {
        using (StreamWriter writer = new StreamWriter(fileStream))
        {
            using (TextWriter originalConsoleOut = Console.Out)
            {
                Console.SetOut(writer);
                Console.WriteLine("Hello To File");
                Console.SetOut(originalConsoleOut);
            }
        }
    }
    Console.WriteLine("Hello to console only");
}