Using LINQ to update string array

前端 未结 3 1553
庸人自扰
庸人自扰 2021-01-18 23:18

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

3条回答
  •  不知归路
    2021-01-19 00:07

    You probably shouldn't try to use LINQ to modify the original query source, because it's awkward and error-prone, and not in the "style" of LINQ.

    Instead, you can use LINQ as a filter to create a new array. All we need to do is rearrange your code a little:

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

提交回复
热议问题