Using LINQ to update an array of strings

I am trying to create a method to update an AssemblyInfo file with a new version using LINQ. I can successfully retrieve the row that I need to update, but don’t know how to update the item in the collection. I am new to LINQ, so any advice is welcome!

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 writing the original file back
        File.WriteAllLines(file, asmInfo);
    }
+5
source share
3 answers

I am new to LINQ, so any advice is welcome!

Linq is primarily intended for querying, aggregating, and filtering data, and not for directly modifying existing data. You can use it to create a modified copy of your data and overwrite the original data with this copy.

, , - Linq.

:

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

.

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

( ) line.

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

. .

File.WriteAllLines(file, asmInfo);

. , .

:

  • , , , , ( ). Linq .
  • linq . linq, .
  • File.ReadAllLines/File.WriteAllLines / . Linq, .

Linq :

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

, .

Linq:

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

Linq, . , File.WriteAllLines IEnumerable<string>. - ToArray. ToArray ( ToList), , .

File.WriteAllLines IEnumerable<string>, , .

Linq - , IEnumerable, Linq.

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

, :

  • , .
  • , .

, yield return line writer.WriteLine. ( ) . , .

IEnumerable yield return, , Linq - , .

+6

Regex :

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

LINQ, , .

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());
}
+3

, LINQ , , "LINQ".

You can use LINQ as a filter instead to create an array new . All we need to do is change your code a bit:

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

    File.WriteAllLines(file, asmInfo);
}
+1
source

All Articles