Best way to embed a list of name/values for use in dictionary look

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a static list of name/value pairs that I need to store with the
application. I would like these to be editable in a file stored with the
executable.

Using the newer default settings was my first thought. My question is
this... Can I access the name by doing a search for the string? What I'm
doing is having the lookup the name at runtime depending on what was returned
in a DataTable and replacing that name with the corresponding value. I can't
use the newer, and very nice, code completed way of pulling my settings.

In short, I'm trying to store a dictionary that I can load at runtime and
use against the data pulled into my DataTable.

Thanks!
 
I have a static list of name/value pairs that I need to store with the
application. I would like these to be editable in a file stored with the
executable.

I assume you mean you want to deploy your exe and another file (eg xxx.txt)
with it, as an auxiliary file. I am assuming that you dont want to carry
xxx.txt internal to the exe. If you want your file internal to the exe, look
to resources.

If you want xxx.txt deployed external to the exe, then add xxx.txt to your
project and make its build action be "Content". In your install project, you
may need to say something about including content files in your install
package (I did this once a while back, and the details are fuzzy - sorry
about that). If you do this right, your exe and xxx.txt will be installed in
the same directory.

Be advised - the exe and xxx.txt are not installed in the same directory in
the development environment, so you will have to be a little clever about
testing. In my code, what I do when I want to read such a file is to check
the current directory, and if not found, check its parent. For me, that
takes care of business for both the ide and deployment environments.
Using the newer default settings was my first thought.

Don't know nothing about it.
My question is
this... Can I access the name by doing a search for the string?
...
In short, I'm trying to store a dictionary that I can load at runtime and
use against the data pulled into my DataTable.

What I would do is read the file at program startup and load it into a
hashtable. I would do the conversions through the hashtable.
 
Thanks for your response. I wound up doing the following:

I decided to add a text file to the project containing my values as you
suggested. The text file is just two comma delimited values per line. The
first value becomes the key in my dictionary while the second value becomes
the value related to the key.

After placing the text in the project, I added a post-build event to copy
the text file to wherever my build target was using the following post-build
command:

copy "$(ProjectDir)*.txt" "$(TargetDir)"

I added a private method returning a Dictionary object that loads the
dictionary from whatever file is supplied. The method I used is as follows:

// Loads a comma delimited, two values per line file into a dictionary

private Dictionary<string, string> LoadCustomDictionaryFromFile(string
FileName)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
if (File.Exists(FileName))
{
using (StreamReader sr = File.OpenText(FileName))
{
string input;
while ((input = sr.ReadLine()) != null)
{
string[] values = input.Split(",".ToCharArray());
dictionary.Add(values[0], values[1]);
}
sr.Close();
}
}
return dictionary;
}
 
Back
Top