每次脚本调用 C# 时递增文件

Increment file everytime script is called C#

我正在从一个统一项目中调用这个脚本。此时我要做的是在每次调用脚本时递增文件。

所以一旦调用它,它就会读取数据并将数据保存在文件中。

现在它只创建一个文件alpha0.csv,如果我再次调用脚本,它不会打印一个新文件。

任何人都可以指导我如何解决这个问题。

using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System.Management;
using Microsoft.Win32;
using System.Collections.Generic;


public class ArduinoControl : MonoBehaviour
{

    SerialPort arduino;
    public string portName = "COM5";
    public static bool status ;
    public string test = "alpha";
    private static int counter;
    private static int lineCount;
    private static string receivedstring = string.Empty;

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


    void Update()
    {
        if (arduino.IsOpen)
        {
            if (status)  // (& UnityCommand = "F")  
            {
                arduino.Write("s");
                arduino.ReadTimeout = 5000;
                arduino.WriteTimeout = 5000;
                receivedstring += arduino.ReadLine() + "\r\n";
                arduino.BaseStream.Flush();
                lineCount++;
                if(lineCount >= 10 && receivedstring != null)
                {
                    WriteOutputToTextFile(test,receivedstring); // Write to csv here...
                    status = false;
                }
                arduino.BaseStream.Flush();

             }

        }
    }


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


}

如果我理解正确的话,您想创建一个具有正确数字后缀的新文件,如 alpha0.csvalpha1.csvalpha2.csv... 这实际上可能有点痛苦,您可以按照注释建议在更广泛的范围内使用变量,但是当您将其保存到磁盘时,我假设您希望它即使在启动和停止程序之间也能正常工作。


我会建议

  1. 确保所有文件都在它们自己的文件夹中
  2. 读入所有文件名
  3. 运行 'find max' 算法对你文件夹中的后缀
  4. 为此加 1 并使用它来创建新文件

可以在 Unity 中读取文件名,如下所示:

DirectoryInfo dir = new DirectoryInfo("/*your directory*/");
FileInfo[] fileInfos = dir.GetFiles("*.csv");

然后获取下一个后缀

static readonly string rootName = "Alpha";
int maxFileNumber = 0;
foreach (var f in fileInfos)
{
    string tempName = f.Name;
    tempName = tempName.Substring(0, tempName.Length - ".csv".Length); // remove .csv
    int lengthOfNumber = tempName.Length - rootName.Length; // get the length of the number at the end of the name
    nextFileNumber = int.Parse(tempName.Substring(rootName.Length, lengthOfNumber)); // get the number at the end of the name
    maxFileNumber = nextFileNumber > maxFileNumber ? nextRoomNumber : maxRoomNumber; // find max alorithm
}
nextFileNumber += 1; // next number
string newFileName = rootName + ToString(nextFileNumber)+".csv";