C# SaveFileDialog in specific folder

試著忘記壹切 提交于 2019-11-30 23:23:40

问题


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

回答1:


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!




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/8789249/c-sharp-savefiledialog-in-specific-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!