1

嘿嘿,我正在尝试确定位图文件的保存进度是否已完成,不能为其使用锁定状态或 UNC 日志。这就是为什么:

运行 FileSystemWatcher 我检测文件的创建和更改(还有文件夹..但它们不是问题)。现在,如果我只是期待复制/移动操作,这整个事情都不会成为问题。当 DCC 应用程序发挥作用时,问题就出现了(让我们以 PhotoShop 为例)。

当您在 PS 中打开文件时,它会锁定它(到目前为止还不错),当您保存文件时,它会保存到临时文件中,然后执行一些“魔术”并获得“新”文件(取决于 PS.version 和文件输出)格式化它直接写入文件而不使用临时文件).. 即使使用临时文件,它也会对文件保持恒定锁定,并且只有在应用程序中关闭文件时才会将 Close 添加到 unc 日志中,而不是在保存之后完成的..

所以你可以在这里看到我的问题。为了在文件被更改(或创建)后进一步处理文件,我需要知道它是否实际上是一个有效的、完整的位图文件。我已经在这方面搜索和阅读了 4 天,并想看看是否有人愿意帮助我。

这是 Watcher 类的简化版本,只有创建和更改处理程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace spacename
{
  public class WatchDog2
  {
    private string RootPath;

    private FileSystemWatcher Watcher;

    // something running in the background chipping away at the cue
    private System.Timers.Timer Clock = new System.Timers.Timer(5000) { AutoReset = true };

    private List<FileSystemEventArgs> FileCheckList = new List<FileSystemEventArgs>();

    private bool CheckLock = false;

    #region Events

    public delegate void OnChangeHandler(object sender, FileSystemEventArgs e);
    public delegate void OnRenameHandler(object sender, RenamedEventArgs e);

    public event OnChangeHandler OnFileCreate;
    public event OnChangeHandler OnFolderCreate;

    public event OnChangeHandler OnFileChange;

    #endregion

    /// <summary>
    /// ctor
    /// </summary>
    public WatchDog2(string rootPath, string filter = "*.*")
    {
      RootPath = rootPath;

      var di = new DirectoryInfo(rootPath);

      if (di.Exists)
      {
        Watcher = new FileSystemWatcher(di.FullName, filter);

        Watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite |
                                NotifyFilters.FileName | NotifyFilters.DirectoryName;

        Watcher.IncludeSubdirectories = true;

        Watcher.InternalBufferSize = 65536; //64kb (max)
      }
      else
      {
        Watcher = new FileSystemWatcher();
      }

      Watcher.Created += Watcher_Created;
      Watcher.Changed += Watcher_Changed;

      Clock.Elapsed += Clock_Elapsed;
    }

    /// <summary>
    /// Create Handler
    /// </summary>
    void Watcher_Created(object sender, FileSystemEventArgs e)
    {
      if (Path.HasExtension(e.FullPath))
      {
        if (App.SupportedExtensions.Contains(Path.GetExtension(e.FullPath).ToLower()))
        {
          if (new FileInfo(e.FullPath).Exists)
          {
            FileCheckList.Add(e);

            Clock.Start();
          }
        }
      }
      else
      {
        if (OnFolderCreate != null)
          OnFolderCreate(sender, e);
      }
    }

    /// <summary>
    /// Change Handler
    /// </summary>
    void Watcher_Changed(object sender, FileSystemEventArgs e)
    {
      if (Path.HasExtension(e.FullPath))
      {
        if (App.SupportedExtensions.Contains(Path.GetExtension(e.FullPath).ToLower()))
        {
          if (new FileInfo(e.FullPath).Exists)
          {
            FileCheckList.Add(e);

            Clock.Start();
          }
        }
      } // no folder change catching
    }

    /// <summary>
    /// Queue check
    /// </summary>
    void Clock_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      if (!CheckLock)
      {
        CheckLock = true;

        for (int i = 0; i < FileCheckList.Count; i++)
        {
          var fi = new FileInfo(FileCheckList[i].FullPath);

          if (fi.Exists)
          {
            if (fi.Length > 0) //add propper checks for bitmap validity
            {
              switch (FileCheckList[i].ChangeType)
              {
                case WatcherChangeTypes.Created:
                  if (OnFileCreate != null)
                    OnFileCreate(null, FileCheckList[i]);
                  break;

                case WatcherChangeTypes.Changed:
                  if (OnFileChange != null)
                    OnFileChange(null, FileCheckList[i]);
                  break;
              }
            }
          }
          else // if save was aborted
          {
            FileCheckList.RemoveAt(i);
          }
        }
        CheckLock = false;
      }
    }
  }
}

差点忘了..我是在 WPF 应用程序的上下文中执行此操作的。

谢谢你花时间:)

4

0 回答 0