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);
}
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.