C# SaveFileDialog 在特定文件夹中

发布于 2024-12-25 14:25:20 字数 281 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏辞 2025-01-01 14:25:20

不,这是不可能的。

您不能直接将其设置为 SaveFileDialog 上的属性。但是您可以尝试使用 FileOk 事件来验证文件是否位于该目录中,否则取消该事件!

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

如前所述,下一个最佳选择是构建您自己的对话框!

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!

薄荷梦 2025-01-01 14:25:20

我想到的一些解决方案是:

选择文件后显示错误

不如从一开始就阻止用户那么好,但它不需要大量代码,而且非常简单。

构建您自己的文件选择屏幕

使之看起来像用户习惯的任何东西是非常痛苦的。需要大量代码。

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.

凤舞天涯 2025-01-01 14:25:20

我能想到的可能是偏离主题的,因为它与编程没有太多关系,而且可能很困难。

安装应用程序时,您应该在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文