Mircea Ispas
Mircea Ispas

Reputation: 20800

C# SaveFileDialog in specific folder

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

Upvotes: 10

Views: 4661

Answers (3)

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

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

Upvotes: -1

Kolky
Kolky

Reputation: 2977

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!

Upvotes: 9

RQDQ
RQDQ

Reputation: 15579

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.

Upvotes: 0

Related Questions