简体   繁体   中英

Not able to set read-only property in CFile in MFC?

I am creating a file which will have some details in it, and I don't want anybody to be able to edit it. So, I decided to keep it as a read-only file. I tried the following code but it's popping up an exception when I set the status.

Please tell me if there's an alternative solution.

Here's my code:

CFile test(L"C:\\Desktop\\myText.txt",CFile::modeCreate|CFile::modeWrite);
CFileStatus status;
test.GetStatus(status);
status.m_attribute = CFile::readonly;
test.SetStatus(L"C:\\Desktop\\myText.txt",status);

Try one of the following:

  1. Close the file before changing the status with a call to CFile::Close() ( test.Close() in your example.)
  2. OR in the readonly attribute with the existing attributes, eg status.m_attribute |= CFile::readonly .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM