我该如何检查文件大小是否比开始时大

本文关键字:是否 文件大小 开始时 检查 何检查 | 更新日期: 2023-09-27 18:25:03

这是代码:

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。所以我想检查一下,如果文件比停止时间2和开始时间1之前的文件大。

但这一行:如果(f.Length>s1)不符合逻辑,因为im一直在做长s1=f.Length;

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

我该如何检查文件大小是否比开始时大

您可以依赖一个全局变量(如您用于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;
        }
    }
}