如何将字符串文件路径用作Environment.SpecialFolder类型? [英] How can I use a string file path as an Environment.SpecialFolder type?

查看:184
本文介绍了如何将字符串文件路径用作Environment.SpecialFolder类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建的程序有问题.我需要能够将FolderBrowserDialog.SelectedPath的字符串输出用作Environment.SpecialFolder类型,但是如果用户在Environment.SpecialFolder枚举中未包含的晦涩位置中选择一个自定义文件夹,则OpenFileDialog会赢.'' t接受文件路径并引发异常.

有没有一种方法可以根据用户选择的路径为Environment.SpecialFolder类型创建新的Enum值?还是缺少我缺少的属性?

I have an issue with a program I am creating. I need to be able to use a string output from the FolderBrowserDialog.SelectedPath as an Environment.SpecialFolder type, but if the user selects a custom folder inside an obscure location not included in the Environment.SpecialFolder Enum, the OpenFileDialog won''t accept the file path and throw an exception.

Is there a way to make a new Enum value for the Environment.SpecialFolder type based upon the path the user selects? Or is there a property I''m missing?

推荐答案

我认为您没有任何丢失的东西.除非您根据GetFolderPath(Environment.SpecialFolder)返回的另一个路径名自己设计和实现文件结构,否则没有预定义的方式来处理文件结构,这可能不是为了与浏览器或其他文件一起使用而设计的.对话.通常,每个软件产品在这些特殊文件夹"中的一个或多个上都有一个固定的子目录,可能是在配置文件,用户的选项等中设置的,而没有与文件对话框的连接. />
如果您确实需要您想要的东西(我建议您认真地考虑一下这个想法,但是它可能有其优点),并且如果您真的想要一个高质量的解决方案,那么我建议您开发自己的类来实现文件对话框( s)可以使用某些顶级目录初始化的方式,因此对话框操作永远不会在其外部导航.

在并非绝对必要的情况下,抛出并处理异常并将其显示给用户以响应用户的错误"决定,这被称为用户洗脑,这是一种不良的UI做法.

—SA
I think there is no anything you are missing. There is no a predefined way to handle file structure the way you want unless you design and implement it by yourself based on one more path names returned by GetFolderPath(Environment.SpecialFolder), probably this method is not designed with intention to be use with browser or other file dialogs. In common practice, each software product has a fixed sub-directory on one or more of these "special folders", perhaps set up in a configuration file, user''s options, etc., with no connection to file dialogs.

If you really need what you want (I advise you critically review this idea, but it might has its merits), and if you really want a high-quality solution, I would suggest you develop your own class(es) implementing file dialog(s) the way it could be initialized with some top level directory, so the dialog operations would never navigate outside of it.

Throwing and handling exception and showing it to the user in response to user''s "wrong" decision in cases when it is not absolutely necessarily is called user brainwashing, a bad UI practice.

—SA


这篇关于如何将字符串文件路径用作Environment.SpecialFolder类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆