我正在使用MFC CFile。
在我的应用程序的Windows OS中,我通过使用共享拒绝访问打开文件来锁定文件。
在同一应用程序或进程中,我必须检查文件是否已锁定?
现在,我知道的唯一方法是打开文件并检查GetLastError()。
还有其他解决方法吗?
答案 0 :(得分:0)
尝试首先打开文件。如果该文件存在并且已经打开且没有共享访问权限,则会出现共享冲突
CFileException::sharingViolation
。
有关CFileException::m_cause
的可能值,请参见CFileException
示例:
UINT open_flag = CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate;
CFileException ex;
if(file.Open(filename, open_flag, &ex))
{
//success
}
else
{
if(ex.m_cause == CFileException::fileNotFound)
{
//file doesn't exit
}
else if(ex.m_cause == CFileException::sharingViolation)
{
//file exists and is locked
}
}