创建一个新的 .txt 文件而不是在 C# 中追加

Create a new .txt file instead of appending in C#

我的串行端口流打印 10 行数据然后暂停,现在我有它 运行 循环所以当我关闭流时我的 .txt 文件看起来像这样:


8205,17.51,150.45,-31.27,-170.05,27.01,160.82,20.90,140.70,9.04,160.07
8283,17.11,149.94,-28.36,-173.83,26.96,158.14,20.59,139.33,9.81,163.18
8360,16.83,149.43,-25.49,-177.23,26.93,155.32,20.23,137.91,10.31,165.87
8438,16.54,148.91,-22.70,179.71,26.95,152.43,19.87,136.46,10.55,168.05
8516,16.25,148.41,-20.05,176.97,27.03,149.46,19.53,134.96,10.62,169.77
8594,15.71,147.75,-17.63,174.62,26.95,146.57,19.11,133.39,10.54,171.02
8672,15.40,147.20,-15.38,172.53,26.92,143.51,18.69,131.79,10.40,172.02
8750,14.97,146.59,-13.37,170.74,27.12,140.25,18.25,130.16,10.22,172.67
8827,14.51,145.95,-11.67,169.22,27.06,137.08,17.78,128.44,10.06,173.09
8905,14.18,145.33,-10.20,168.01,27.04,133.74,17.29,126.68,9.92,173.41

10373,5.38,128.09,-4.03,163.82,12.60,58.36,3.28,81.27,9.29,173.94
10450,4.90,126.76,-4.03,163.87,11.06,58.47,2.17,77.95,9.32,173.97
10528,4.41,125.37,-4.04,163.89,11.26,58.00,0.99,74.50,9.29,173.94
10605,3.95,123.91,-4.03,163.87,11.21,58.24,-0.25,70.91,9.29,173.89
10683,3.51,122.40,-4.06,163.88,11.88,58.18,-1.56,67.21,9.28,173.87
10760,3.10,120.81,-4.05,163.88,11.69,58.15,-2.94,63.38,9.32,173.93
10838,2.65,119.18,-4.05,163.87,10.90,58.80,-4.43,59.36,9.34,173.95
10915,2.25,117.48,-4.07,163.90,10.59,58.80,-6.03,55.20,9.37,173.93
10992,1.84,115.70,-4.09,163.96,11.04,58.75,-7.78,50.88,9.39,173.95
11070,1.45,113.87,-4.08,163.97,11.54,58.72,-9.65,46.35,9.37,173.94

11147,1.40,113.60,-4.07,163.97,10.89,59.06,-11.14,43.04,9.38,173.98
11225,1.08,111.68,-4.11,164.01,11.78,58.57,-13.41,38.18,9.39,174.02
11302,0.72,109.66,-4.11,164.03,12.08,58.60,-15.97,33.17,9.37,174.00
11379,0.38,107.58,-4.11,164.01,11.28,59.05,-18.44,28.89,9.35,173.95
11457,0.11,105.39,-4.10,164.06,10.20,59.66,-18.48,29.09,9.37,174.00
11535,-0.16,103.13,-4.12,164.12,10.37,59.57,-18.00,29.31,9.41,173.98
11613,-0.38,100.75,-4.13,164.14,10.58,59.73,-18.61,29.05,9.41,174.04
.
.
.
.
.

.

我想做的不是追加到同一个文件中。我正在尝试创建一个编号递增的新文件。 例如 txt01,txt02,txt03.....txtn.

谁能帮我解决这个问题

using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
using System.IO;

public class ArduinoControl : MonoBehaviour
{
    public string portName = "COM5";
    public string receivedstring;
    SerialPort arduino;

    void Start()
    {
        arduino = new SerialPort(portName, 115200);
        arduino.Open();
    }

    void Update()
    {
        if (arduino.IsOpen)
        {

            arduino.Write("s");
            receivedstring = arduino.ReadLine();

            WriteOutputToTextFile(receivedstring); // Write to csv here...
            arduino.BaseStream.Flush();
        }



    }

    static void WriteOutputToTextFile(string _data)
    {
        string FolderName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);   //set destination as your desktop
        using (StreamWriter SW = new StreamWriter(FolderName + "\test.txt", true))   //true makes it append to the file instead of overwrite
        {
            SW.WriteLine(_data);
            SW.Close();
        }
    }
}

你可以用这个

void Start()
{
    receivedstring= string.Empty;
    arduino = new SerialPort(portName, 115200);
    arduino.Open();
}

private static int counter;
static void WriteOutputToTextFile(string _data)
{
     string FolderName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);   //set destination as your desktop
      using (StreamWriter SW = new StreamWriter(${FolderName}\test{counter}.txt", false))
      {
         SW.WriteLine(_data);
         SW.Close();
      }
      counter++;
      lineCount=0;
      receivedstring= string.Empty;
}

private static int lineCount;
void Update()
{
    if (arduino.IsOpen)
    {
         arduino.Write("s");
         receivedstring += arduino.ReadLine() + "\r\n";
         lineCont++;
        If(lineCount >=10 && !string.IsNullOrEmpty (receivestring))
             WriteOutputToTextFile(receivedstring); // Write to csv here...
        arduino.BaseStream.Flush();
    }



}