LANCommander/LANCommander.SDK/Helpers/TextFileHelper.cs

28 lines
810 B
C#
Raw Permalink Normal View History

2025-11-26 18:45:19 -06:00
using System.IO;
using System.Text.RegularExpressions;
using AutoMapper;
2025-11-26 18:45:19 -06:00
namespace LANCommander.SDK.Helpers;
internal static class TextFileHelper
{
internal static string ReplaceAll(string path, string regexPattern, string substitution, RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase)
2025-11-26 18:45:19 -06:00
{
path = Regex.Replace(path, @"[/|\\]", Path.DirectorySeparatorChar.ToString(), options);
2025-11-26 18:45:19 -06:00
if (!File.Exists(path))
throw new FileNotFoundException("File not found", path);
var text = File.ReadAllText(path);
var update = Regex.Replace(text, regexPattern, substitution, options);
if (text != update)
{
File.WriteAllText(path, update);
return update;
2025-11-26 18:45:19 -06:00
}
return text;
2025-11-26 18:45:19 -06:00
}
}