檢查檔案的A C M time,確保其時間與當下時間差大於指定的秒數
public static bool CheckFileInUse(Logs logtext, string filePath, int timeout)
{
if( !EFCS.FileExist(logtext, filePath, Conf.RUNNING_NO_CMD_MESSAGE) )
return false;
DateTime lastAT = System.IO.File.GetLastAccessTime(filePath);
DateTime createT = System.IO.File.GetCreationTime(filePath);
DateTime lastWT = System.IO.File.GetLastWriteTime(filePath);
DateTime nowT = DateTime.Now;
if( (nowT - lastAT).TotalSeconds < 0 ||
(nowT - createT).TotalSeconds < 0 ||
(nowT - lastWT).TotalSeconds < 0 )
{
System.IO.File.SetCreationTime(filePath, DateTime.Now);
System.IO.File.SetLastAccessTime(filePath, DateTime.Now);
System.IO.File.SetLastWriteTime(filePath, DateTime.Now);
}
lastAT = System.IO.File.GetLastAccessTime(filePath);
createT = System.IO.File.GetCreationTime(filePath);
lastWT = System.IO.File.GetLastWriteTime(filePath);
nowT = DateTime.Now;
if( (nowT - lastAT).TotalSeconds < timeout ||
(nowT - createT).TotalSeconds < timeout ||
(nowT - lastWT).TotalSeconds < timeout )
{
logtext.Out("[Info ] [" + filePath + "] creation/modification time interval < " + timeout + " secs");
return true;
}
return false;
}
此外,另增加了File Lock檢查
public static bool CheckLock(Logs logtext, string _strSourceFileName)
{
int i = 0;
bool bResult = true;
while( i < 20 )//Retry 20次
{
try
{
using( Stream stream = System.IO.File.Open(_strSourceFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite) )
{
if( stream != null )
{
logtext.Out("[Info ] [" + _strSourceFileName + "] is ready.");
bResult = false;
break;
}
}
}
catch( FileNotFoundException ex )
{
logtext.Out("[Info ] [" + _strSourceFileName + "] is not ready. (" + ex.Message + ")");
bResult = true;
}
catch( IOException ex )
{
logtext.Out("[Info ] [" + _strSourceFileName + "] is not ready. (" + ex.Message + ")");
bResult = true;
}
catch( UnauthorizedAccessException ex )
{
logtext.Out("[Info ] [" + _strSourceFileName + "] is not ready. (" + ex.Message + ")");
bResult = true;
}
finally
{
i++;
System.Threading.Thread.Sleep(500);
}
}
return bResult;
}
沒有留言:
張貼留言