Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to check a file with the file path. Is there any way to get the user permission granted or not for that particular file path? In generally, can i get any information is the file is editable or not using C# code?
Posted
Comments
labshasanbd 17-Dec-11 5:39am    
There are many users "My Documents" folders in my PC. When i am trying to execute File.Exists(filename) it returns false though it presents there for that user. Can i make a decision from here, that it's not editable/accessible?

The only way to absolutely check if a file can be edited is to open a stream for read/write, within a try...catch block. If you don't get an exception, it is editable. Remember to Close and Dispose the stream immediately you are finished checking, or it will not be available because the check code has locked it!

However, that does not stop the file becoming unavailable immediately afterwards! (In fact, in some systems, it may make it more likely top be in use afterwards, as some backup systems watch for file access and immediately grab the file for backing up...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Dec-11 1:21am    
Agree, my 5. I means: use offensive programming, not defensive. (Also, remember the old wisdom: best kind of defense is the offense. :-)
--SA
OriginalGriff 18-Dec-11 4:13am    
"Offensive Programming" :laugh:
Don't we get enough of that here anyway, SA?
Check the file is readonly[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900