文件的上次修改时间不变

时间:2017-12-05 07:37:16

标签: c# .net system.io.file

我在Timer的Tick事件中编写了以下代码,以检查图像的文件修改时间是否已更改,然后复制该图像并将其保存到另一个目录。有时GetLastWrittenTime()即使文件已被覆盖也不会更改。

 if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CaptureMerge.bmp"))
{   
     DateTime filetime=File.GetLastWriteTime(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CaptureMerge.bmp");
     if (!filetime.Equals(lastmMdifiedTime))
     {
          if (txtkapan.Text != "" && txtlot.Text != "")
          {
              if (reg.Read("AUTOPATH") == null || reg.Read("AUTOPATH").Equals(""))
              {
                  Directory.CreateDirectory("D:\\" + txtkapan.Text + "\\" + txtlot.Text);
              }
              else
              {
                  Directory.CreateDirectory(reg.Read("AUTOPATH")+"\\" + txtkapan.Text + "\\" + txtlot.Text);
              }
              string image_name;
              if (txtlot.Text.Length <= 3 || txtparty.Text.Length <= 3 || txtkapan.Text.Length <= 3)
              {
                  image_name = txtparty.Text.Substring(0, txtparty.Text.Length) + "_" + txtkapan.Text.Substring(0, txtkapan.Text.Length) + "_" + txtlot.Text.Substring(0, txtlot.Text.Length) + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm") + ".jpg";
              }
              else
              {
                  image_name = txtparty.Text.Substring(0, 3) + "_" + txtkapan.Text.Substring(0, 3) + "_" + txtlot.Text.Substring(0, 3) + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm") + ".jpg";
              }
              try
              {
                  System.Drawing.Image img = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CaptureMerge.bmp");
                  img.Save(reg.Read("AUTOPATH") + "\\" + txtkapan.Text + "\\" + txtlot.Text + "\\" + image_name, ImageFormat.Jpeg);
                  lastmMdifiedTime = File.GetLastWriteTime(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CaptureMerge.bmp");
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Exception:" + ex.ToString());
              }
        }
        else  { }
  }
}

请告诉我我的代码有什么问题。

0 个答案:

没有答案