Search code examples
c#app-configgetfiles

How to place search pattern in app.config in C#


I have the following loop:

foreach (var midtier in midtierpath.GetFiles("logfile?.log").Take(1))  //search pattern

The search pattern looks for files from logfile0.log to logfile9.log What I would like to do is, instead of having the search pattern hardcoded ("logfile?.log") in my foreach, to place it in the app.config. I already tried the following:

< appSettings>
    < add key="myFile" value="logfile?.log"/>
< /appSettings>

But whenever I'm trying to use the key, it doesn't recognise it as a search pattern but as the actual file name. Can you please tell me if it's possible to do this or not?


Solution

  • You can access Web.Config key name like this:

    string FilePath = ConfigurationManager.AppSettings["myFile"].ToString();
    

    Now you can call this in your foreach.