यहाँ एक समाधान है जो कुछ प्रयोक्ताओं के लिए overkill हो सकता है। मैंने एक नई स्थैतिक कक्षा बनाई है जिसमें एक ऐसी घटना है जो फ़ाइल को कॉपी करने पर ही तभी ट्रिगर होती है।
उपयोगकर्ता फ़ाइलों जो वे FileAccessWatcher.RegisterWaitForFileAccess(filePath)
फोन करके देखना पसंद करेंगे, पंजीकृत करता है। फ़ाइल पहले से ही देखा नहीं किया जा रहा है एक नया कार्य शुरू कर दिया है जो बार-बार फ़ाइल की जाँच करता है देखने के लिए अगर इसे खोला जा सकता है। प्रत्येक बार जब यह जांचता है तो यह फ़ाइल आकार भी पढ़ता है। यदि पूर्व-निर्धारित समय (फ़ाइल मेरे उदाहरण में 5 मिनट) में फ़ाइल का आकार नहीं बढ़ता है तो लूप निकल जाता है।
जब फ़ाइल से पाश बाहर निकलता सुलभ किया जा रहा है या समय समाप्ति से FileFinishedCopying
घटना शुरू हो रहा है।
public class FileAccessWatcher
{
// this list keeps track of files being watched
private static ConcurrentDictionary<string, FileAccessWatcher> watchedFiles = new ConcurrentDictionary<string, FileAccessWatcher>();
public static void RegisterWaitForFileAccess(string filePath)
{
// if the file is already being watched, don't do anything
if (watchedFiles.ContainsKey(filePath))
{
return;
}
// otherwise, start watching it
FileAccessWatcher accessWatcher = new FileAccessWatcher(filePath);
watchedFiles[filePath] = accessWatcher;
accessWatcher.StartWatching();
}
/// <summary>
/// Event triggered when the file is finished copying or when the file size has not increased in the last 5 minutes.
/// </summary>
public static event FileSystemEventHandler FileFinishedCopying;
private static readonly TimeSpan MaximumIdleTime = TimeSpan.FromMinutes(5);
private readonly FileInfo file;
private long lastFileSize = 0;
private DateTime timeOfLastFileSizeIncrease = DateTime.Now;
private FileAccessWatcher(string filePath)
{
this.file = new FileInfo(filePath);
}
private Task StartWatching()
{
return Task.Factory.StartNew(this.RunLoop);
}
private void RunLoop()
{
while (this.IsFileLocked())
{
long currentFileSize = this.GetFileSize();
if (currentFileSize > this.lastFileSize)
{
this.lastFileSize = currentFileSize;
this.timeOfLastFileSizeIncrease = DateTime.Now;
}
// if the file size has not increased for a pre-defined time limit, cancel
if (DateTime.Now - this.timeOfLastFileSizeIncrease > MaximumIdleTime)
{
break;
}
}
this.RemoveFromWatchedFiles();
this.RaiseFileFinishedCopyingEvent();
}
private void RemoveFromWatchedFiles()
{
FileAccessWatcher accessWatcher;
watchedFiles.TryRemove(this.file.FullName, out accessWatcher);
}
private void RaiseFileFinishedCopyingEvent()
{
FileFinishedCopying?.Invoke(this,
new FileSystemEventArgs(WatcherChangeTypes.Changed, this.file.FullName, this.file.Name));
}
private long GetFileSize()
{
return this.file.Length;
}
private bool IsFileLocked()
{
try
{
using (this.file.Open(FileMode.Open)) { }
}
catch (IOException e)
{
var errorCode = Marshal.GetHRForException(e) & ((1 << 16) - 1);
return errorCode == 32 || errorCode == 33;
}
return false;
}
}
उदाहरण उपयोग:
// register the event
FileAccessWatcher.FileFinishedCopying += FileAccessWatcher_FileFinishedCopying;
// start monitoring the file (put this inside the OnChanged event handler of the FileSystemWatcher
FileAccessWatcher.RegisterWaitForFileAccess(fileSystemEventArgs.FullPath);
FileFinishedCopyingEvent संभाल:
private void FileAccessWatcher_FileFinishedCopying(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File finished copying: " + e.FullPath);
}
स्रोत
2016-10-14 04:35:40
संभव डुप्लिकेट [क्या कोई फ़ाइल जांचने का कोई तरीका है?] (http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in -use) –
इस कोड में 'फ़ाइल। क्रेट (फ़ाइल नाम)' के साथ एक साधारण बग है। जवाब उस बिंदु को याद कर रहे हैं। बंद होने की प्रतीक्षा करना जरूरी नहीं है। – usr