Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ to update string array

I'm trying to create a method to update an AssemblyInfo file with a new version string, using LINQ. I can successfully extract the string I need to update, but am not sure how to update the item in the collection. I'm new to LINQ, so any advice is appreciated!

private void UpdateVersion(string file, string version)
    {
        string[] asmInfo = File.ReadAllLines(file);
        var line = asmInfo.Single(x => x.Trim().StartsWith("[assembly: AssemblyVersion"));
        line = "[assembly: AssemblyVersion\"" + version + "\")]";

        // This doesn't work - it's writing the original file back
        File.WriteAllLines(file, asmInfo);
    }
like image 269
Marcus K Avatar asked Jun 18 '11 05:06

Marcus K


2 Answers

I'm new to LINQ, so any advice is appreciated!

Linq is mostly designed around querying, aggregating, and filtering data, not around directly modifying existing data. You can use it to make a modified copy of your data, and overwrite the original data with this copy.

The problem you're having isn't exactly because of Linq, though.

Lets go over your code:

string[] asmInfo = File.ReadAllLines(file);

You're copying all the lines of the file into an in-memory string array.

var line = asmInfo.Single(x => x.Trim().StartsWith("[assembly: AssemblyVersion"));

You're querying all the lines, and copying out the line (more-or-less by value) into the variable line.

line = "[assembly: AssemblyVersion\"" + version + "\")]";

You're writing over your copied line with different content. This does not modify the original array.

File.WriteAllLines(file, asmInfo);

You're writing back the original array to the file. You didn't change the array, so you won't see any change in the file.

A few ways to solve this:

  • Copy out the array, as you're currently doing, find the index of the line you want to change, and modify the array (by index). This option doesn't use Linq at all.
  • Use linq to make a transformed copy of the data, and write the whole transformed copy back. This option uses linq, but doesn't leverage its power.
  • Stop using File.ReadAllLines/File.WriteAllLines, and read/write one line at a time. This option actually leverages the power of Linq, but is the most complicated.

Modify the array

This method doesn't use Linq at all:

string[] asmInfo = File.ReadAllLines(file);
int lineIndex = Array.FindIndex(asmInfo,
    x => x.Trim().StartsWith("[assembly: AssemblyVersion"));
asmInfo[lineIndex] = "[assembly: AssemblyVersion\"" + version + "\")]";
File.WriteAllLines(file, asmInfo);

This is a fairly standard way to accomplish the task, assuming your file sizes are small.

Query and make a transformed copy of the data, and write out the copy

This method is more Linq-like:

string[] asmInfo = File.ReadAllLines(file);
var transformedLines = asmInfo.Select(
    x => x.Trim().StartsWith("[assembly: AssemblyVersion")
        ? "[assembly: AssemblyVersion\"" + version + "\")]"
        : x
    );
File.WriteAllLines(file, asmInfo.ToArray());

This is more Linq-like than the previous example. But it is actually less efficient because File.WriteAllLines cannot take an IEnumerable<string>. Because of this, you are forced to call ToArray. When you call ToArray (or ToList), the entire query is run and copied into a new array, so you have two copies of the file sitting around.

If File.WriteAllLines took an IEnumerable<string>, then you wouldn't have to make an extra copy, and each line would be written while the query was still running.

Read and write one line at a time

This option is the most efficient, and actually shows some of the benefits of Linq - more specifically, of IEnumerable, which is where most of Linq's power comes from.

public static class FileStreamExtensions
{
    public static IEnumerable<string> GetLines(this FileStream stream)
    {
        using (var reader = new StreamReader(stream))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
                yield return line;
        }
    }

    public static void WriteLines(this FileStream stream, IEnumerable<string> lines)
    {
        using (var writer = new StreamWriter(stream))
        {
            foreach (string line in lines)
            {
                writer.WriteLine(line);
            }
        }
    }
}

private void UpdateVersion(string file, string version)
{
    using (FileStream inputFile = File.OpenRead(file))
    {
        string tempFilePath = Path.GetTempFileName();

        var transformedLines = inputFile.GetLines().Select(
            x => x.Trim().StartsWith("[assembly: AssemblyVersion")
                ? "[assembly: AssemblyVersion\"" + version + "\")]"
                : x
            );

        using (FileStream outputFile = File.OpenWrite(tempFilePath))
        {
            outputFile.WriteLines(transformedLines);
        }

        string backupFilename = Path.Combine(Path.GetDirectoryName(file), Path.GetRandomFileName());
        File.Replace(tempFilePath, file, backupFilename);
    }
}

This requires a lot more code because:

  • The intermediate copy of your data is now in a temp file, instead of an in-memory array.
  • The stream classes don't themselves provide line-by-line enumerators, so we built them ourselves.

You'll see some interesting things if you run it in the debugger, and set break points on the yield return line and writer.WriteLine statements. The read code and the write code are (more or less) running at the same time. First a line is read, then it is written.

This is because of IEnumerable and yield return, and this is one of the main reasons Linq is more than just syntactic sugar.

like image 55
Merlyn Morgan-Graham Avatar answered Sep 25 '22 16:09

Merlyn Morgan-Graham


I suggest using Regex for this:

private static void UpdateVersion(string file, string version)
{
    var fileContent = File.ReadAllText(file);
    var newFileContent = Regex.Replace(
        fileContent,
        @"(?<=AssemblyVersion\("")(?<VersionGroup>.*?)(?=""\))",
        version);
    File.WriteAllText(file, newFileContent);
}

You can still do this with LINQ, but this will be less efficient and more error-prone, I guess.

private static void UpdateVersion(string file, string version)
{
    var linesList = File.ReadAllLines(file).ToList();
    var line = linesList.Single(x => x.Trim().StartsWith("[assembly: AssemblyVersion"));
    linesList.Remove(line);
    linesList.Add("[assembly: AssemblyVersion(\"" + version + "\")]");
    File.WriteAllLines(file, linesList.ToArray());
}
like image 43
Alex Aza Avatar answered Sep 23 '22 16:09

Alex Aza