I have a Program, it's C# Console App, The function of the program is to open a file and search for a specific line and edit it.
I can run the program without Parameter and the local file will be edited, or i can run the program with a parameter to Specify the path of the file I want to modify, or i can run the program with a parameter to get help.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace BuildIncrement
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(args.Length);
string filePath = "version.h";
if (args.Length > 0)
{
if (args[0].Contains("path"))
{
var argsArray = args[0].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (argsArray.Length > 1)
{
filePath = argsArray[1];
}
}
else if (args[0].Contains("help"))
{
Console.WriteLine("To run the Program please type --path");
return;
}
}
if (File.Exists(filePath) == false)
{
Console.WriteLine("Fehler: Die Datei existiert nicht!");
return;
}
List<string> lines = File.ReadAllLines(filePath).ToList();
string line = lines.Where(i => i.Contains("BUILD_NUM")).FirstOrDefault();
//foreach (string line in lines)
int buildNr = 0;
if (line != null)
{
string[] lineparts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (lineparts.Length > 2)
{
string Nr = lineparts[2];
if (int.TryParse(Nr, out buildNr))
{
}
buildNr++;
int index = lines.IndexOf(line);
lines.RemoveAt(index);
lines.Insert(index, line.Replace(Nr, buildNr.ToString()));
}
}
File.WriteAllLines(filePath, lines);
Console.WriteLine($"Build-Nr wurde auf {buildNr} aktualisiert!");
}
}
}