这是代码:
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之前的文件。
但这一行:if(f.Length> s1)不符合逻辑,因为我一直在做长s1 = f.Length;
如何检查文件是否大于文件?
答案 0 :(得分: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;
}
}
}