Forum Discussion
AlexKaras
14 years agoCommunity Hero
Hi Ofer,
For one of my C# projects I ended-up with the code below. It works so far. I did not try to port it to TestComplete, but think that this should be possible (with the use of aqFile and/or dotNet objects).
Maybe somebody have better suggestion?
//-------------------------------------------------------------------------------
/// <summary>
/// Tries to move file with the check whether it is locked.
/// Code is based on: http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c
/// Also: http://stackoverflow.com/questions/685135/open-file-in-exclusive-mode-in-c
/// </summary>
/// <param name="sourceFileName">The full filename of the original file to move</param>
/// <param name="targetFileName">The full filename of the target file</param>
/// <returns>Nothing</returns>
private bool MoveFile(string sourceFileName, string targetFileName)
{
const int _numberOfTries = 60;
const int _timeIntervalBetweenTries = 500;
bool bRetVal = false;
int tries = 0;
while (true)
{
try
{
File.Move(sourceFileName, targetFileName);
return true;
return File.Open(_fileName, FileMode.Open, fileAccess, Fileshare.None);
}
catch (IOException e)
{
if (!IsFileLocked(e))
throw;
if (++tries > _numberOfTries)
throw new Exception(
String.Format(@"The file {0} is locked too long by the {1} process: {2}",
sourceFileName, GetFileProcessName(sourceFileName), e.Message),
e);
Thread.Sleep(_timeIntervalBetweenTries);
}
}
return bRetVal;
}
//-------------------------------------------------------------------------------
/// <summary>
/// Checks whether the exception was caused by the file being locked.
/// </summary>
/// <param name="exception">The exception occurred during file operation</param>
/// <returns>True if file is locked, False otherwise</returns>
private static bool IsFileLocked(IOException exception)
{
int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
return (32 == errorCode) || (33 == errorCode);
}
//-------------------------------------------------------------------------------
For one of my C# projects I ended-up with the code below. It works so far. I did not try to port it to TestComplete, but think that this should be possible (with the use of aqFile and/or dotNet objects).
Maybe somebody have better suggestion?
//-------------------------------------------------------------------------------
/// <summary>
/// Tries to move file with the check whether it is locked.
/// Code is based on: http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c
/// Also: http://stackoverflow.com/questions/685135/open-file-in-exclusive-mode-in-c
/// </summary>
/// <param name="sourceFileName">The full filename of the original file to move</param>
/// <param name="targetFileName">The full filename of the target file</param>
/// <returns>Nothing</returns>
private bool MoveFile(string sourceFileName, string targetFileName)
{
const int _numberOfTries = 60;
const int _timeIntervalBetweenTries = 500;
bool bRetVal = false;
int tries = 0;
while (true)
{
try
{
File.Move(sourceFileName, targetFileName);
return true;
return File.Open(_fileName, FileMode.Open, fileAccess, Fileshare.None);
}
catch (IOException e)
{
if (!IsFileLocked(e))
throw;
if (++tries > _numberOfTries)
throw new Exception(
String.Format(@"The file {0} is locked too long by the {1} process: {2}",
sourceFileName, GetFileProcessName(sourceFileName), e.Message),
e);
Thread.Sleep(_timeIntervalBetweenTries);
}
}
return bRetVal;
}
//-------------------------------------------------------------------------------
/// <summary>
/// Checks whether the exception was caused by the file being locked.
/// </summary>
/// <param name="exception">The exception occurred during file operation</param>
/// <returns>True if file is locked, False otherwise</returns>
private static bool IsFileLocked(IOException exception)
{
int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
return (32 == errorCode) || (33 == errorCode);
}
//-------------------------------------------------------------------------------