C# SaveFileDialog in specific folder
Asked Answered
E

3

10

I use SaveFileDialog to select the path where I want to save a file. I set InitialDirectory to some folder, but I want to limit the save locations to that folder or subfolders of that folder. Is this possible?

SaveFileDialog dialog = new SaveFileDialog();
dialog.InitialDirectory = "SomePath"//this is the path that I want to be root folder
Euphroe answered 9/1, 2012 at 13:37 Comment(2)
Sorry, but I can't think about a case when this should be useful. Either you give the user the control over where to save or you just save the file. Could you please explain why you want to do this?Ferdinand
I have an Editor that is used to edit files from a bundle(it will be copied to a device as it is). I don't want to let the user to be able to save files in other folder - they will not be copied at the end and this may lead to weird errors.Euphroe
M
9

No it is not possible.

You can't directly set this as a Property on the SaveFileDialog. But you can try to do it by using the FileOk event to validate if the file is in that directory and otherwise cancel the event!

dialog.FileOk +=
    delegate (object sender, CancelEventArgs e)
    {
        if (dialog.FileName is in wrong directory)
        {
            e.Cancel = true;
        }
    };

As mentioned, the next best option is to build your own Dialog!

Mamey answered 9/1, 2012 at 13:43 Comment(2)
I don't want to cancel if the user selects file from other folder, I don't want to let him chose other folder.Euphroe
You will only cancel the selection, you will not cancel the dialog.Mamey
F
0

Some solutions that come to mind are:

Display an error after file selection

Not as nice as preventing the user in the first place, but it doesn't take a lot of code and is pretty straightforward.

Build your own file selection screen

Very painful to make look like anything that the user is accustomed to. Takes a lot of code.

Formate answered 9/1, 2012 at 13:45 Comment(0)
S
0

What i can think of might be off-topic because it's not related as much with Programming and it might be difficult.

While you're application is being installed ,you should create a Specific User on Windows just for you're application.

Than you can start you're App. as that user using App. Manifest File.

After that you can give that Specific user permission's to write only at the root folder ,this how the OS will control that.

PS : I don't think if this solution will pay it self ,but it might work.

Salute

Sausa answered 9/1, 2012 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.