Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shannon Broskie - 08 Feb 2006 13:57 GMT
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!
AMercer - 08 Feb 2006 15:39 GMT
> 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.
Shannon Broskie - 08 Feb 2006 20:30 GMT
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;
}

> > 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
[quoted text clipped - 30 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.