find whether a folder has read/write permission?

  • Thread starter Thread starter Chen Zhuo
  • Start date Start date
C

Chen Zhuo

Hi,

I need to write some code to test whether a folder has read/write
permission. The idea is to try to read sub-directories in this folder,
if succeed, then test whether it's read-only. Any exception thrown means
we don't have permission to access this folder:

bool IsDirectoryAccessible(string path)
{
try
{
bool retVal = false;
System::IO::DirectoryInfo* dirInfo = new
System::IO::DirectoryInfo(Marshal::ToString(path));

dirInfo->GetDirectories();
// GetDirectories() throw Exception if the folder is not accessible

if(IsReadOnly(path))
retVal = true;

return retVal;
}
catch (System::Exception* ex)
{
Util::Managed::Rethrow(ex);
}
}



But I think this algorithm is not so straight-forward. Anyone have
better idea of testing the folder accessibility?

Thanks a lot
 
Take a look at the FileIOPermission class.

Also, if you need some examples in C++, you might need to try another group.
 
Thanks.. but still not sure how to get the current user's permission of
the specified folder? Could you give me some code segment?
Thanks ya

Btw, seems that GetDirectories() is too powerful... even I set C:\test
to be non-access to anyone, this API still can get the sub directories
under it!...
 
Back
Top