views:

47

answers:

3

Hello,

I'm needing to create a application that will read a file, that will be like this:

Font = "Courier New"
Size = "10"
Style = "Bold"

As you can see I'm going to use this to set some properties of a TextBox, but I'm going to store each thing that is inside these quotes and store they on some variables(textFont, textSize, textStyle).

I think that I can use this syntax to open a stream with the file:

StreamReader reader = new StreamReader("confs.txt");

The questions are:

  • As the StreamReader is declared it will read the file in the application directory?(Just to Confirm)
  • How can I read only what is inside the quotes and store each one in a string?

As I think, file I/O is iqual in Windows Mobile and Windows. Best Regards

+2  A: 

It's better to declare the full path, or use the following syntax:

System.IO.Path.Combine((new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath, "confs.txt");

I think to parse it, you would have an easier time saving the file as Xml and loading it into an XmlDocument. Or better yet, abandon this idea and store tine information in the app.config file. It's so much easier and you still get to edit it in notepad without recompiling if you need to.

(which would render the first half of the answer as moot)

David Stratton
+2  A: 

1: as long as you don't change the current directory, yes. There are more thorough ways to get the app directory, though

2: any number ways to parse such a string; string.Split, Regex, etc; purely as an illustration, I'll (ab)use DbConnectionStringBuilder:

using (StreamReader reader = File.OpenText("data.txt")) {
    string line;
    DbConnectionStringBuilder db = new DbConnectionStringBuilder();
    while ((line = reader.ReadLine()) != null) {
        db.ConnectionString = line;
        foreach (string key in db.Keys) {
            Console.WriteLine(key);
            Console.WriteLine(db[key]);
        }
    }
}

edit DbConnectionStringBuilder may not exist in windows-mobile; perhaps just Split on = or use Regex; it comes down to the exact format.

Marc Gravell
As Maxwell Smart would say - Would you believe, beaten by that much!
Wayne
@Wayne: huh? ..
Marc Gravell
Your post was same as time as mine, but your solution looked more elegant than my "old-fashioned" way!
Wayne
+3  A: 

How about something like this:

foreach(string s in FileLines) {
  string[] data = s.Split(new[] { '=' });
  // property is in data[0]
  // value is in data[1]
}

What you have created is a simple CSV file

Where by CSV I mean Character Separated Value...

Update

Ok, seems like you are trying to copy exactly instead of understanding the algorithm so I will create a new one here

StreamReader reader = new StreamReader("confs.txt");
while(reader.peek >= 0) {
   string l = reader.ReadLine();
   string[] data = l.Split(new[] { '=' });
   // property is in data[0]
   // value is in data[1]
}
reader.Close();
reader.Dispose();
Wayne
I think it's stretching the definition to call this a CSV file.
pavium
I understand, but I would not call it a configuration file as there are no sections for each of the keys
Wayne
Filelines was an example array of strings. This was an example on how to split the string. I am sure you know how to read a file line by line or slurp into an array yes?
Wayne
Thanks very much for your patience. **;)**
Nathan Campos
string l = reader.ReadLine();Should bestring s = reader.ReadLine();
David Glass
Oh, shoot! Thanks for that David. Adjusting...
Wayne