0

这是代码:

private void timer2_Tick(object sender, EventArgs e)
        {
            timerCount += 1;
            TimerCount.Text = TimeSpan.FromSeconds(timerCount).ToString();
            TimerCount.Visible = true;
            if (File.Exists(Path.Combine(contentDirectory, "msinfo.nfo")))
            {
                string fileName = Path.Combine(contentDirectory, "msinfo.nfo");
                FileInfo f = new FileInfo(fileName);
                long s1 = f.Length;
                if (f.Length > s1)
                {
                    timer2.Enabled = false;
                    timer1.Enabled = true;
                }
            }
        }

一旦文件存在,它的大小约为 1.5mb,但几分钟后文件会更新到几乎 23mb。所以我想检查文件是否大于停止 timer2 并启动 timer1 之前的文件。

但是这一行: if (f.Length > s1) 不合逻辑,因为我一直在做 long s1 = f.Length;

我如何检查文件是否比原来的大?

4

1 回答 1

1

您可以依赖一个全局变量(如您使用的contentDirectory那个)来存储最后观察到的大小。示例代码:

public partial class Form1 : Form
{
    long timerCount = 0;
    string contentDirectory = "my directory";
    long lastSize = 0;
    double biggerThanRatio = 1.25;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        timerCount += 1;
        TimerCount.Text = TimeSpan.FromSeconds(timerCount).ToString();
        TimerCount.Visible = true;
        if (File.Exists(Path.Combine(contentDirectory, "msinfo.nfo")))
        {
            string fileName = Path.Combine(contentDirectory, "msinfo.nfo");
            FileInfo f = new FileInfo(fileName);
            if (f.Length >= biggerThanRatio * lastSize && lastSize > 0)
            {
                timer2.Enabled = false;
                timer1.Enabled = true;
            }

            lastSize = f.Length;
        }
    }
}
于 2013-07-25T10:06:38.953 回答