bala
bala

Reputation: 375

How to check folder have write permission or not in c++

I would like to write a file inside a "d:\test\" unfortunately I don't have a write permission for that folder. How to check whether that folder have a write permission or not.

NOTE: fopen is helpful,But that's creates a new file. I don't want to create exra file.

I am also seen CreateFile(), don't know how to use that for this case.

Upvotes: 3

Views: 12709

Answers (3)

mike.dld
mike.dld

Reputation: 3049

Just open (and write to) the file and analyse the error returned by the system, if any. Checking if you have rights in a separate operation would result in race condition, where first call tells you you could create a file while second one fails because access rights have changed inbetween.

Upvotes: 2

RedX
RedX

Reputation: 15184

Actually one way of checking if you have permission to write is to use the security access functions. There you can query for all the different rights (delete, create, write, index...)

I have never used them so i can't provide you with an example.

Remember though that between checking your access permissions and creating the file or writing there could be a change in access rights and it could still fail. The race condition mentioned by the others.

If you will end up creating a file anyway, then just use CreateFile as mentioned by the others.

Upvotes: 0

Digital_Reality
Digital_Reality

Reputation: 4758

Alternative 1:

You can use _access or _access_s to check if file is having permission.

More information here

00 Existence only
02 Write-only
04 Read-only
06 Read and write

Alternative 2:

As you already tried CreateFile, use GENERIC_WRITE as your dwDesiredAccess

and then see error, if any.

More details here

PS: If you anyway just need to check (and not write/create file), delete temp file, if already created.

Upvotes: 2

Related Questions