问题
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()...
回答1:
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.
来源:https://stackoverflow.com/questions/3579394/application-settings-directoryinfo-fileinfo