Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:27:14+00:00 2026-05-23T06:27:14+00:00

I’m trying to create a method to update an AssemblyInfo file with a new

  • 0

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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:27:15+00:00Added an answer on May 23, 2026 at 6:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.