C# 文本文件始终为空

C# 文本文件始终为空,c#,C#,我创建了这个timer类,当调用它的end timer函数时,它应该编写一个文本文件,但是每次它似乎只创建一个空白文件。你知道为什么吗?我已经检查过,修改的日期始终是我上次运行函数的时间 public class Timer100NanoSeconds { long startTick; List<Tuple<string, long>> stepTicks; readonly string functionName; int timesR

我创建了这个timer类,当调用它的end timer函数时,它应该编写一个文本文件,但是每次它似乎只创建一个空白文件。你知道为什么吗?我已经检查过,修改的日期始终是我上次运行函数的时间

public class Timer100NanoSeconds
{
    long startTick;
    List<Tuple<string, long>> stepTicks;
    readonly string functionName;
    int timesRan;
    public Timer100NanoSeconds(string name)
    {
        functionName = name;
        StartNew();
    }

    public void StartNew()
    {
        timesRan = 0;
        startTick = DateTime.Now.Ticks;
        stepTicks = new List<Tuple<string, long>>();
    }

    public void AddStep(string temp)
    {
        stepTicks.Add(new Tuple<string, long>(temp, DateTime.Now.Ticks));
    }
      public void counterForTimesRan(int ran)
    {
        timesRan = ran;
    }
    public void EndTimer()
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(functionName + ".txt"))
        {
            file.WriteLine("Ran " + timesRan + " Times");
            for (int i = 0; i < stepTicks.Count; i++)
            {
                if (i == 0)
                {
                    if (stepTicks[i].Item2 - startTick != 0)
                    {
                        file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                    }
                }
                else
                {
                    if (stepTicks[i].Item2 - stepTicks[i - 1].Item2 != 0)
                    {
                        file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                    }
                }
            }
        }
    }

    public void EndTimer(bool showZero)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(functionName + ".txt"))
        {
            for (int i = 0; i < stepTicks.Count; i++)
            {
                if (showZero)
                {
                    if (i == 0)
                    {
                        file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                    }
                    else
                    {
                        file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                    }
                }
                else
                {
                    if (i == 0)
                    {
                        if (stepTicks[i].Item2 - startTick != 0)
                        {
                            file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                        }
                    }
                    else
                    {
                        if (stepTicks[i].Item2 - stepTicks[i - 1].Item2 != 0)
                        {
                            file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + "   ,  " + stepTicks[i].Item1.Trim());
                        }
                    }
                }
            }
        }
    }

}

你是否调用过EndTimer?你确定你的if语句中有一条被击中了吗?使用计时器的代码在哪里?它是否调用EndTimer?DateTime.Now.Ticks不会提供100纳秒的分辨率,-请使用Stopwatch类进行精确计时。