-1

I have a Windows App that takes a word and searches pdf files for that word. I'm being asked to expand those words into an alias that also looks for other keywords for that alias. So if a user is searching for "Toyota" the program knows to search also for "Camry", "Corolla", and "Prius".

I need to create an external file to my C# program that works as a list of aliases and keywords. The external file needs to be an .ini file. Nothing I can do about it being in an .ini file; that's what the client wants.

So, I need an .ini file that works something like this:

Toyota = Camry
Toyota = Corolla
Toyota = Prius

Then, when Toyota is type in a text box, the program uses the .ini file to know that it also needs to use Camry, Corolla, and Prius.

Can someone give me code that will be able to do this? Thank you.

boilers222
  • 1,901
  • 7
  • 33
  • 71
  • 1
    possible duplicate of [Reading/writing INI file in C#](http://stackoverflow.com/questions/217902/reading-writing-ini-file-in-c-sharp) – rene Aug 22 '12 at 13:26
  • 2
    @rene: I think "real" INI files may not contain keys multiple times within the same section, so this might not be a duplicate. – Jens Aug 22 '12 at 13:27
  • 4
    Why INI? Don't you see that the format you chose doesn't fit the problem? Use XML instead which supports collections neatly. – dzendras Aug 22 '12 at 13:29
  • 1
    that could easily be solved if the value is used as as a string which is parsed using a delimiter, for example a pipe (|) so the value pair would look like toyota = – wterbeek Aug 22 '12 at 13:30
  • 1
    @dzendras, as Boilers specified in the question the client wants to use an ini file not an xml file, even though that might be easier to use – wterbeek Aug 22 '12 at 13:31
  • @Jens there is nothing that prevent you from having multiple keys under an application section. But I admit, it makes the usage of the GetPrivateProfileString pretty useless. – rene Aug 22 '12 at 13:33
  • Thanks wterbeek. If I have toyota= in an .ini file, how do I read that file? Say the user typed in toyota, how do I get the values camry, corolla, prius into variables to use them? – boilers222 Aug 22 '12 at 13:33
  • @wterbek: Clients usually don't know what they want. I just feel pity for a developper that gets that code in a few years (== after a few more change requests)... – dzendras Aug 22 '12 at 13:36
  • @boilers222: Look at the link rene posted. That should help you to read ini files. – Jens Aug 22 '12 at 13:40

1 Answers1

0

You can read whole ini file at program startup (you can check how here). Then store key values in some container similar to this:

Dictionary<string, List<string>>

where keys are for example "Toyota", and list of values are "Camry", "Corrola", "Prius"...

Krešimir Lukin
  • 399
  • 1
  • 7