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

Time:12-23

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

Sorry I'm new to C#

GProvider.conf =

Host:127.0.0.1
Port:29300
PwVersion:156

CodePudding user response:

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.

CodePudding user response:

so there are only three lines in your file.

string host = gproviderConfs[0].Split(':')[1];
int port = int.Parse(gproviderConfs[1].Split(':')[1]);
int version = int.Parse(gproviderConfs[2].Split(':')[1]);

you need to get the value after the :, there are many ways to do this.

You can also get the value after the first : of each line. In this example I have spliitted the value by : and taking the second item

int port = int.Parse(gproviderConfs[1].Substring(gproviderConfs[1].IndexOf(':')   1));
  •  Tags:  
  • c#
  • Related