PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Best way to embed a list of name/values for use in dictionary look

Reply

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

 
Thread Tools Rate Thread
Old 08-02-2006, 01:57 PM   #1
=?Utf-8?B?U2hhbm5vbiBCcm9za2ll?=
Guest
 
Posts: n/a
Default Best way to embed a list of name/values for use in dictionary look


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!
  Reply With Quote
Old 08-02-2006, 03:39 PM   #2
=?Utf-8?B?QU1lcmNlcg==?=
Guest
 
Posts: n/a
Default RE: Best way to embed a list of name/values for use in dictionary look

> 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.

  Reply With Quote
Old 08-02-2006, 08:30 PM   #3
=?Utf-8?B?U2hhbm5vbiBCcm9za2ll?=
Guest
 
Posts: n/a
Default RE: Best way to embed a list of name/values for use in dictionary

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;
}



"AMercer" wrote:

> > 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.
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off