C# SaveFileDialog 在特定文件夹中
我使用 SaveFileDialog
来选择要保存文件的路径。我将 InitialDirectory
设置为某个文件夹,但我想将保存位置限制为该文件夹或该文件夹的子文件夹。这可能吗?
SaveFileDialog dialog = new SaveFileDialog();
dialog.InitialDirectory = "SomePath"//this is the path that I want to be root 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这是不可能的。
您不能直接将其设置为
SaveFileDialog
上的属性。但是您可以尝试使用FileOk
事件来验证文件是否位于该目录中,否则取消该事件!如前所述,下一个最佳选择是构建您自己的对话框!
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 theFileOk
event to validate if the file is in that directory and otherwise cancel the event!As mentioned, the next best option is to build your own Dialog!
我想到的一些解决方案是:
选择文件后显示错误
不如从一开始就阻止用户那么好,但它不需要大量代码,而且非常简单。
构建您自己的文件选择屏幕
使之看起来像用户习惯的任何东西是非常痛苦的。需要大量代码。
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.
我能想到的可能是偏离主题的,因为它与编程没有太多关系,而且可能很困难。
安装应用程序时,您应该在 Windows 上为您的应用程序创建一个特定用户。
然后你就可以启动你的应用程序了。作为使用应用程序的用户。清单文件。
之后,您可以授予特定用户仅在根文件夹中写入的权限,这就是操作系统控制的方式。
PS:我不认为这个解决方案会自己支付费用,但它可能会起作用。
敬礼
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