John Riker

John Riker

  • NA
  • 85
  • 15.6k

Adding string array in config file

Oct 29 2020 10:22 AM
I have an app that loops thru video files of various extensions and converts them.  I had the list of extensions hard coded during testing:
  1. string[] extensions = { ".mp4"".mov"".avi"".mpg"".wmv"".mkv"".m4v" };  
That is used in my loop as:
  1. foreach (string file in Directory.EnumerateFiles(rootfolder, "*.*", searchOption)   
  2. .Where(s => extensions.Any(ext => ext == Path.GetExtension(s))))  
Works fine. 
 
I tried adding it to my appname.config in several ways:
  1. <xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <startup>   
  4.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />  
  5.     </startup>  
  6.     <appSettings>    
  7.       <add key="extensionstest" value="".mp4",".mov",".avi",".mpg",".wmv",".mkv",".m4v"" />  
  8.     </appSettings>    
  9. </configuration>  
Tried the above with ampersand quote semicolon and without any quotes so with the period and the extension name but it doesn't seem to parse it on output.  It may actually do the first one but not 100% sure.   
 
Tried retrieving it in several fashions but latest was:
  1. string[] extensions = ConfigurationManager.AppSettings["extensionstest"].Split(',');  
Any help with the most efficient way to do this or do I need to hard code this and recompile if new extensions come up I need to handle?
 
Thanks.
JR 

Answers (4)