程序在打开一个文件后,怎样禁止其他程序删除、修改?
怎样限制:
比如:
CFileDialog dlg(TRUE, NULL, NULL,OFN_READONLY | OFN_OVERWRITEPROMPT, Filetype);if(dlg.DoModal() == IDOK)
{
CString filename = dlg.GetPathName();
…????
OpenDocumentFile(filename);
}

解决方案 »

  1.   

    CFile file;
    file.Open(filename,CFile::modeRead|CFile::shareDenyWrite);
      

  2.   

    CFile file;
    file.Open(filename,CFile::modeRead|CFile::shareDenyWrite);
      

  3.   

    试试 CFileDialog::OnShareViolation 
      

  4.   

    to:: shilong(星矢の诗龙) 
    好像不行,限制不住;
    to:: yinx(yinx) 
    CFileDialog::OnShareViolation 
    不会用
    谢谢
      

  5.   

    to : shilong(星矢の诗龙) 
    还是不行,限制不住
    to:: yinx(yinx) 
    CFileDialog::OnShareViolation 
    怎么用?
    谢谢
      

  6.   

    CFile::shareExclusive   
    Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.CFile file;
    file.Open(filename,CFile::modeRead|CFile::sshareExclusive);
      

  7.   

    但是我现在已经加了
    file.Open(filename,CFile::modeRead|CFile::sshareExclusive);

    file.Open(filename,CFile::modeRead|CFile::shareDenyWrite);

    可在外部还是可以修改和删除文件;
    请教各位高手!
      

  8.   

    使用
    LockFile()
    UnLockFile()
      

  9.   

    HANDLE CreateFile(
      LPCTSTR lpszName,         
      DWORD fdwAccess,          
      DWORD fdwShareMode,       
      LPSECURITY_ATTRIBUTES lpsa,   
      DWORD fdwCreate,          
      DWORD fdwAttrsAndFlags,   
      HANDLE hTemplateFile      
    )
    用这个函数,我刚试过了,应该可以