G
G
Gatix2022-02-02 22:36:28
C++ / C#
Gatix, 2022-02-02 22:36:28

How to store program settings without external files (C#)?

We need a way to somehow write the program settings data without creating external files, the settings must be written once and for all into the program (I think to implement it by passing it to a parameter when calling the program in the console). I thought about the option to keep these settings data in the source code itself, but how can I write something else into the program code, so that after execution with a certain parameter, the build would come out with the necessary settings written in the code?
Edit: I've heard of such a thing as Source Generator, would this work for my task?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
G
Gatix, 2022-02-03
@Gatix

The problem is solved with the help of Mono.Cecil, I create a config class in the finished build, and through another build, using Mono.Cecil I change the first build so that the necessary data is stored in the config

G
Griboks, 2022-02-02
@Griboks

You can store program settings in completely different places. But if you don't like specific files, then just hide them (in the properties attribute "hidden").
But don't hide the settings files, because obviously users will need them.

M
Myclass, 2022-02-03
@Myclass

It is not entirely clear who enters these settings? Each computer has its own? How many such computers? Is it impossible at the compilation level to create for each computer its own version of the exe-shnik? It's much easier this way - different types are created in one place. After any new functions, for example, 100 copies of this exe are created. For each computer - its own. From there, it is distributed over computers in a ready-made format. This process can be automated. And all the different configurations are saved centrally too. In a file or database - at the discretion.

K
Konstantin Tsvetkov, 2022-02-03
@tsklab

Creation of resource files .

A
Alexander, 2022-02-03
@Adler_lug

If you dodge a lot, then you can make it so that some arbitrary data is added to the overlay of the file being launched, and then subtracted from there. How to do this is only limited by your imagination.
Something dynamically changing so naturally cannot be saved, because. it's just inconvenient, but something that you can save and forget once.

V
Vladimir Korotenko, 2022-02-03
@firedragon

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var server = "https://ya.ru";
            var template = [email protected]"namespace StsServerIdentity {{
    public class Settings
    {{
        public const string Server = ""{server}"";
        public const int Port = 80;
        public const string User = """";
        public const string Password = """";
    }}
}}
";
            Console.WriteLine(template);
        }
    }
}

I think the idea is clear. Modify for yourself, for example, by adding the processing of arguments and run the program and the subsequent build of the project in a cycle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question