0

So I want to execute my program with an .exe, and I would like to configure my program without actually write into the code.

I have some yes-no questions in my code, like: What would you like to zip? File/Directory. Edit: I don't want to answer these questions in the console application, I would like to answer these before, like before "settings".

And my question is, can I answer these questions without writing into the code, and make this app executable in this way? Are there any programs for it?

Thanks for the answers!

11
  • 2
    What to you mean by Without writing into it ? Commented Jan 17, 2020 at 11:12
  • 1
    There's a wide range of storage available from a simple structured Xml or Json file to a real database of any flavor. Commented Jan 17, 2020 at 11:15
  • 1
    I think you mean command line arguments - see learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…. Commented Jan 17, 2020 at 11:16
  • 1
    @Polyfun No, not command line arguments - just a configurable data source of items to run thru. Commented Jan 17, 2020 at 11:17
  • 1
    look into application settings, data persistance, registry.. Commented Jan 17, 2020 at 11:18

1 Answer 1

2

You have 2 approaches you can use depending on your usage preference, the first suggestion is in case you are using your program and you don't set the values too often

  1. You can use app.config file and add relevant values and call them via your code as variables.

  2. You can write to a xml file or json file a small configuration file abd edit it easily and it is also good for clients using your app to change configuration easily via configuration file.

To do this try use xml serialisation and deserialisation object, I'll add code sample if required.

Edit to use external configuration you need the next classes: 1. Configuration data object

[Serializable]
public class Configuration : ICloneable
{
    public Configuration()
    {
        a = "a";
        b= "b"
    }

    public string a { get; set; }
    public string b { get; set; }

    public object Clone()
    {
        return new Configuration
        {
            a = a,
            b= b
        };
    }
}
  1. File write and read class

    public class ConfigurationHandler
    {
     // full path should end with ../file.xml
     public string DefaultPath = "yourPath";
    
    public ConfigurationHandler(string path = "")
    {
        if (!File.Exists(DefaultPath))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath));
            FileStream file = File.Create(DefaultPath);
            file.Close();
    
            Configuration = new Configuration();
            SaveConfigurations(DefaultPath);
        }
    }
    
    public void SaveConfigurations(string configPath = "")
    {
        if (string.IsNullOrEmpty(configPath))
            configPath = DefaultPath;
        var serializer = new XmlSerializer(typeof(Configuration));
        using (TextWriter writer = new StreamWriter(configPath))
        {
            serializer.Serialize(writer, Configuration);
        }
    }
    
    
    public Configuration LoadConfigurations(string configPath = "")
    {
        if (string.IsNullOrEmpty(configPath))
            configPath = DefaultPath;
        using (Stream reader = new FileStream(configPath, FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
            Configuration = (Configuration)serializer.Deserialize(reader);
        }
        return Configuration;
    }
    

    }

  2. to get the configuration instance you can use it from your program:

    static void Main(string[] args) {

    var config = new ConfigurationHandler().LoadConfigurations();
    //....
    

    }

Sign up to request clarification or add additional context in comments.

2 Comments

My first idea also was json, I am trying to figure it out how it actually works. I am new to C# and to .NET.
@TinM Serializing a list to JSON can give you some clues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.