简体   繁体   中英

Read from resx file at runtime

I have a list of strings I'm keeping a .resx file that I would like to use to populate a list during runtime. I was planning on using a ResXResourceReader . I'm not sure what the filepath to my resource file would be, but following the directions here , an exception is throwing saying that it can't find the .resx file in the bin directory corresponding to my selected build target. Is my resource file somewhere else that I can access during run time or do I need to change the build action on this resource file so it appears somewhere that I can reach at run time? Thanks in advance!

In source explorer right click the file and select properties. There is a setting called "Copy to Output Directory" it typically defaults to do not copy. Change it to copy always or copy if newer.

On a side note, I don't think you're using the resx as intended. I believe that is more used as a compile time resource, you can access the values in the file in a static manner in your source. Instead, if you want an extra, more dynamic resource file, you should just add a flat file to the project (with data in your preferred format) and set copy to output directory to copy if newer.

EDIT: After reading the comments I want to reiterate that this is an abuse of the resx and not it's intended use. I highly recommend using the suggestion provided in the second paragraph (personally I would use a text file with json and parse it into objects uses json.net's generic deserialize method), the first bit applies to all types of files and is just a way for you to interact with the build script (in this case the csproj file) via Visual Studio's GUI.

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