0

We've got a custom .settings file which we use to store variables and custom actions defined under certain circumstances.

Basically all we have is a format right now and no way of reading it. I did propose an idea but it was quickly turned down and I was told that we "must use Linq".

Now I'm sitting here wondering how I am going to do this. The file we have looks like this:

[SET Name:"NeverAcquireRemoteFiles" Value:"False"]
[SET Name:"CheckForUpdates" Value:"True"]
[SET Name:"" Value:""]
[SET Name:"" Value:""]

[PERFORM Action:"AcquireRemoteFiles" When:"ApplicationWindowShown"]
[PERFORM Action:"CheckForUpdates" When:""]
[PERFORM Action:"" When:""]
[PERFORM Action:"" When:""]
[PERFORM Action:"" When:""]
[PERFORM Action:"" When:""]
[PERFORM Action:"" When:""]
[PERFORM Action:"" When:""]

Don't mind the empty lines, they're just there to demonstrate that there is no specific amount of lines, set's or actions in this file.

To be honest, I don't know enough of Linq to figure this out (without needing a bit more time) and I just don't see how I could write a query against this file.

The lines beginning with SET are variables. The Name attribute represents the name of the variable, while the Value representing it's value (duh). Lines beginning with PERFORM are the actions. I think you know the rest.

Basically, we have certain "stages" in our application where we may or may not need to do certain things. And these aren't necessarily things that can be automated with 100% accuracy. And we need 100% accuracy. So hence the idea of a settings file. Based on the conditions and current circumstances, we amend or remove from or add to the settings file before we launch the application.

Is it possible to read this file using Linq, if so, how?

spike.y
  • 389
  • 5
  • 17
  • Is that a legacy format? Because if not, you should be using XML... And then you can use Linq-to-XML – Matthew Watson May 07 '14 at 11:44
  • If you don't know enough of Linq, then it's OK. But you should know at least something, and probably you already tried it. Please, add your code – Sergey Berezovskiy May 07 '14 at 11:44
  • There's not enough information to give a definite answer. We see the input here, but what is the output? We can't define output for you... – Dialecticus May 07 '14 at 11:46
  • possible duplicate of [C# Reading a File Line By Line](http://stackoverflow.com/questions/1271225/c-sharp-reading-a-file-line-by-line) –  May 07 '14 at 11:47
  • This is a legacy format. I have suggested XML (and even JSON) last year. Our bosses love the old stuff I guess. I have tried using string operations like Split and LastIndexOf etc, I did not try regex because frankly, I think that's an ugly solution. Basically, we just need to be able to read the file, loop through each line and determine if it's a SET or a PERFORM, if it's a set, split it up into Name and Value - if it's a PERFORM, split it up into an Action and When (string values only). – spike.y May 07 '14 at 11:51

1 Answers1

4

I suggest you to use regular expressions to parse lines from your config file. E.g. getting variables settings will look like:

var regex = new Regex(@"\[SET Name:""(?<name>\w+)"" Value:""(?<value>\w*)""\]");

var variables = File.ReadLines("legacy.config")
                    .Select(l => regex.Match(l))
                    .Where(m => m.Success)
                    .Select(m => new {
                        Name = m.Groups["name"].Value,
                        Value = m.Groups["value"].Value
                    }).ToArray();

Or query syntax

var variables = from l in File.ReadLines("legacy.config")
                let m = regex.Match(l)
                where m.Success
                select new {
                    Name = m.Groups["name"].Value,
                    Value = m.Groups["value"].Value
                };

Output:

{ Name = "NeverAcquireRemoteFiles", Value = "False" }
{ Name = "CheckForUpdates", Value = "True" }

Spend some time, and adopt same approach for parsing actions settings.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459