(C #) How do you check if a path is a protected path on Windows that requires a height change?

We have a folder where our application will potentially read, write, and create / delete files, and while we can default to a safe location, we allow the user to select a different directory. How can we check if the selected directory is "safe"?

We would not require elevation of administrator rights at runtime, and this path will be used for subsequent launches of the application, so it is not enough for us to check if our process can currently write to this place, because it can that the application is launched with administrator rights the first times (this actually happens when the installer launches, since the installer runs at an elevated level), and if they choose a protected location, then the next time they try to launch the application, they won't have access to the directory. Potentially GB of persistent data is stored in this location, so requesting a new location and moving files is not ideal.

0


a source to share


3 answers


Any folder can have some permissions, but they can be changed at any time, including when checking the file and using it. So really the only thing you can do is just use the file and handle the exception if it doesn't work.



+1


a source


Since any folder can have permissions set, but the administrator sees fit, the only real way to know is to know which user it will launch as (currently logged in user?) And then perform an access check to see if the user has the appropriate access to this directory.



+1


a source


Other advice about changing permissions at any time is valid, but checking the installation time is even better than not checking at all.

Since the first run will be promoted, what you probably want to do is start a separate process without the promotion to check the location, and then the original instance can continue to understand that the location will work unless / until someone changes the permissions.

0


a source







All Articles