简体   繁体   中英

How can I store an Environment.SpecialFolder path as a string and then parse it back to Environment.SpecialFolder?

I am developing a WinForms application that stores the user's settings in a text file. I reference this save file when the user updates their settings as well as when the program starts up. Everything is working with the StreamWriters and StreamReaders , except that when I write data to the text file, it is automatically converted to a string since .txt files don't hold variable types.

One of the settings is the default directory to open files from, and the OpenFileDialog dialog only accepts Environment.SpecialFolder file paths. I'm having trouble parsing the string to a usable Environment.SpecialFolder . I realize I could check the value using the string.Contains(string s) method but I would like to know if there is a simpler way first.

SpecialFolder is an enumeration; to parse enumeration values, use Enum.Parse(typeof(Environment.SpecialFolder), "ValueGoesHere")

It sounds like you've written your own classes to manage user settings in a text file. You might want to consider using the application settings classes to do that for you. I think they handle enumerations for you, but I'm not sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM