高分请教:打开文件时:“在访问 xxx.exe时发生共享违例”,详见帖子
一个简单的下载程序,有如下代码
void Download(const char *pMsg, DWORD len)
{
srand(time(NULL));
static fileIndex = rand();
char fileName[256] = {0};
sprintf(fileName, "%u.exe", fileIndex);
static DWORD totalSize = 22222;
CFile file;
CFileException err;
if ( !file.Open(fileName, CFile::modeWrite|CFile::modeCreate|CFile::typeBinary|CFile::modeNoTruncate, &err) )
{
TCHAR errInfo[1024];
err.GetErrorMessage(errInfo, 1024);
AfxMessageBox(errInfo);
}
else
{
static DWORD fileOffset = 0; //当前文件偏移
DWORD regSize = 1024; //每次请求的大小
file.SeekToEnd();
file.Write(pMsg,len); //写入接收到的数据
file.Close();
fileOffset += len; //文件偏移
if ( fileOffset >= totalSize )
{
AfxMessageBox(_T("download finish"));
// SendDownRequest(fileOffset, regSize); ----------语句A
}
else
{
if ( (totalSize - fileOffset) <regSize )
{
regSize = totalSize - fileOffset;
}
SendDownRequest(fileOffset, regSize); //请求后边的数据
}
}
}
以共享方式打开
CFile::shareDenyNone Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
[解决办法]
PathFileExists 先检查下文件是否存在,如果存在换名
[解决办法]