应用程序设置 +目录信息/文件信息

发布于 2024-09-16 01:00:49 字数 1011 浏览 1 评论 0原文

我对 C# 还是新手...我正在构建一个 WPF 应用程序,并尝试应用一些用户应用程序设置。插入标准应用程序设置(int、double、string 等)很容易。我什至插入了类似 WindowState 的东西。

我想要一个 DirectoryInfo 和/或 FileInfo 作为可保存的设置而不是字符串。

所选类型:System.IO.File 给出错误消息“不支持抽象类型”。

这是有道理的,因为如何将抽象类型实现为设置。

所选类型:System.IO.FileInfo 给出错误消息“Type 'System.IO.FileInfo' 未定义。”。

DirectoryInfo/FileInfo 是否无法设置为应用程序设置?是否可以?值得花时间吗?如何确定什么可以用作设置,什么不能用作设置?

我在用户设置方面的经验有限,我正在尝试扩展我的知识,但这让我感到困惑。

编辑:我试图发布一些屏幕截图,但显然我太新了。我在 Visual Studio 的应用程序设置中工作。

进一步说明:

http://msdn.microsoft.com/en-us/library /a65txexh.a​​spx

应用程序设置可以存储为任何可 XML 序列化的数据类型或具有实现 ToString/FromString 的 TypeConverter。最常见的类型是字符串、整数和布尔值,但您也可以将值存储为颜色、对象或连接字符串。

DirectoryInfo di = new DirectoryInfo(@"C:\");
di.ToString();

我是否错过了一些东西,因为它有 ToString()...

I'm still new to C#... I'm building a WPF application and I'm trying to apply some User Application Settings. It's easy to insert standard App Settings (int, double, string, etc). I've even got something like WindowState inserted.

I'd like to have a DirectoryInfo and/or FileInfo as savable settings instead of Strings.

Selected type: System.IO.File gives an error message "Abstract types are not supported".

Which makes sense, since how can you implement an abstract type as a setting.

Selected type: System.IO.FileInfo gives an error message of "Type 'System.IO.FileInfo' is not defined.".

Is DirectoryInfo/FileInfo not settable as App Settings? Is it possible? Worth the time? How can you determine what is usable as a setting and what isn't?

My experience with user settings is limited and I'm trying to expand upon my knowledge and this has me stumped.

edit: I tried to post some screenshots, but apparently I'm too new. I'm working inside of Visual Studio, Application Settings.

further notes:

http://msdn.microsoft.com/en-us/library/a65txexh.aspx

Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string.

DirectoryInfo di = new DirectoryInfo(@"C:\");
di.ToString();

Am I missing something, as it has ToString()...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

万水千山粽是情ミ 2024-09-23 01:00:49

是的,这是不可能的。应用程序设置使用 XML 序列化进行序列化。类可序列化的一项硬性要求是它需要有一个无参数构造函数。两个班级都没有。

这不是一个真正的问题,因为这两个类都有一个接受字符串的构造函数。因此,将设置设置为字符串,您始终可以从中获取 FileInfo 或 DirectoryInfo。尽管它必须引用存在的文件系统对象。如果这是一个问题,那么就创建自己的类。

Yes, this is not possible. Application settings are serialized using XML serialization. One hard requirement for a class to be serializable is that it needs to have a parameter-less constructor. Neither class has one.

This isn't a real problem because either class has a constructor that takes a string. So, make the setting a string and you can get always get a FileInfo or a DirectoryInfo out of it. Albeit that it must refer to a file system object that exists. If that's an issue then just make your own class.

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