C#C
C#2y ago
Runnarin

✅Refactor C# code

guys
my code works, but i'm doubting that i made the most performant way to do it
        public struct Proc
        {
            public string Name;
            public int ID;
            public string? Owner;
        }
....................
  public static void SaveAppsList()
        {
            if (File.Exists(FILE_PATH)) File.Delete(FILE_PATH);

            using StreamWriter writer = new(FILE_PATH);
            foreach (Proc item in ListOfProcs)
                writer.WriteLine($"{item.ID}^{item.Name}^{item.Owner}");
        }
        public static void LoadAppsList()
        {
            if (!File.Exists(FILE_PATH)) return;
            string readText = File.ReadAllText(FILE_PATH);

            if (readText.Trim() == string.Empty) return;

            readText = readText.Replace("\r\n", "\n");
            List<string> lines = [.. readText.Split('\n')];
            foreach (string line in lines)
            {
                if (line.Trim() == string.Empty) continue;
                string[] processInfo = line.Split('^');
                Proc proc = new()
                {
                    ID = int.Parse(processInfo[0]),
                    Name = processInfo[1],
                    Owner = processInfo[2]
                };
                ListOfProcs.Add(proc);
            }
        }

btw, i hate my own code, i know i can do better, but idk how
Was this page helpful?