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
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:
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
. 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
, 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 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 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:
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.