C++\cli Check if file can be created here, FolderBrowseDialog

Go To StackoverFlow.com

0

I'm using a folderBrowseDialog to give the user the ability to choose where the program is going to save some files. there is a problem that the user can choose path that the application can not manipulate, for instance "c:\" (the program is launched by user not admin)

I've seen some program that propose me change my path to "my documents" whenever I try to choose a path that USER cannot manipulate, however I could not figure out how to do it. note that I'm using folderBrowseDialog not savefiledialog.

So I thought that maybe I'll pop a warning, using the FileIOPermission. but for some reasone it doesn't work. here is a quick code I wrote, but the exception is never thrown. FileIOPremission on MSDN

using namespace System::Security::Permissions;

FileIOPermission^ f2 = gcnew FileIOPermission( FileIOPermissionAccess::Write,"C:\\");

try {
    f2->Demand();
}
catch (System::Security::SecurityException^ s){
    MessageBox::Show(s->Message); 
}
2012-04-04 20:21
by Kirill Kulakov


1

You are on the wrong track with this. Yes, a FileIOPermission doesn't do what you hope it does, that's a security related class that allows you to write code that runs in a sandbox. Like code that runs in a browser, stopping it from splattering files all over the user's disk. Or reading sensitive files. That doesn't work in a vanilla C++/CLI app, it runs in full trust, giving the user access to all the directories and files she normally has access to without adding further restrictions. You have no reason to add extra ones.

Writing files to a disk is in general a perilous operation. Lots of things can go wrong. You are running your code on an operating system that has many other processes that are accessing files. Trying to overwrite a file that's being read by some other process is going to cause an exception. The disk might fill up. The user might have entered the name of directory that she doesn't have access to, that's just one more failure mode. Just catch the IOException and tell the user about it.

And sure, start the journey in the dialog from a directory that a restricted user is well familiar with. Her Documents folder is her "home" directory. If it isn't obvious that you're going to dump a whole bunch of files in the selected directory then display a gentle reminder if you see any other files in the directory. Avoids the "Eek, what did you DO!" response when she finds out that here Documents directory suddenly has a lot more files. The first few times. Also don't hesitate to create your own sub-folder when you can give it a descriptive name so this doesn't happen. Some name that has the date/time in it usually works.

2012-04-04 21:12
by Hans Passant
Well, your answer is not very helpful. I'm just trying to create simple program that saves a screen shoots. I asked how do I check whether I have permission to write in this folder or not - Kirill Kulakov 2012-04-05 13:22
Hmm, I answered that. Catch the IOException. You can make it arbitrarily more complicated by using DirectoryInfo::GetAccessControl(). That adds a lot of code but doesn't stop you from having to catch the exception for other reasons that you cannot check up front. You'll need to write that code to understand why the answer was helpful - Hans Passant 2012-04-05 13:28
Ads