Reputation: 384
I want to customize my OpenFileDialog a bit so that it can only access one of my network PC (tsclient) and not access my local drives and downloads folder.
I have no idea to achieve this. So, i am using a temporary alternative which sets the by default location to network but does not block local resource usage.
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts);
Can anyone tell me how to block local resource usage but let the network resources accessable?
Upvotes: 0
Views: 2003
Reputation: 30813
Want you can do after the user clicks "OK/Open" to any of the file is to check the selected filepath
(or FileName
).
If the filepath
is not the network filepath
, you should reject it and ask the user to repeat its choice.
For instance, if Z:\
is your network folder, then you can code something like this,
bool accepted;
do {
accepted = false;
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = ofd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) {
accepted = ofd.FileName.Substring(0, 3) == "Z:\\"; //change this to your network folder
if (accepted) {
//accepted network folder, do something
} else {
//accepted network folder, gives warning with message box
}
} else if (result == System.Windows.Forms.DialogResult.Cancel) {
accepted = true; //if the user chooses cancel, he can go out of the loop
}
} while (!accepted); //prevents unaccepted answer
Edit:
The above example is limited only to give the main idea that you could check what filepath
is selected by the user before approving it. It is understood that Z:\
may not always be where the network folder is.
In case you need more dynamic way to determine whether a drive is in the network or not, I recommend you to look at DriveInfo.DriveType.
In case you need even more robust way for checking, you could create client-specific list of accessible folders in one of your newly-defined config file.
And in case you have multiple users having different privileges, you will need to check the privileges of the users together with the folder accessed before determining whether the user can proceed using the file or not.
In all cases, they share the same main idea: the user input must be first accepted before the user can proceed. This main idea is the one that is demonstrated in my example above.
Hope it may clarify
Upvotes: 1