Search code examples
c#stringinteger

What is the correct way to write a .conf file?


I'm trying to compile a ready-made project, in which a .conf file is missing, I tried to write this file but I always get errors like "System.FormatException: Input string was not in a correct format." What is the correct way to write the .conf file for this code? From what I've seen, you have to declare a value for Host: 127.0.0.1; Port: 29300; PwVersion : 156;


namespace CoreAutoMessage.Models;

public record GProvider : IPwDaemonConfig
{
    public string Host { get; private init; }
    public int Port { get; private init; }
    public int PwVersion { get; private init; }

    public GProvider()
    {
        var gproviderConfs = File.ReadAllLines("./Configurations/GProvider.conf");

        this.Host = gproviderConfs[1];
        this.Port = int.Parse(gproviderConfs[3]);
        this.PwVersion = int.Parse(gproviderConfs[5]);
    }
}

GProvider.conf =

Host:127.0.0.1
Port:29300
PwVersion:156


Solution

  • Here is the format:

    Host: or literally anything here.
    127.0.0.1
    Port:
    29300
    PwVersion:
    156
    

    The ReadAllLines method returns an array of strings, literally lines, separated by endline character. And in your code you are just looking for specified line numbers, 1 3 5, numeration from 0.