kiddo
kiddo

Reputation: 1626

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);

Upvotes: 1

Views: 1496

Answers (1)

Rob
Rob

Reputation: 78658

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, e.g. status.m_attribute |= CFile::readonly.

Upvotes: 2

Related Questions