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!

Bizhan
  • 16,157
  • 9
  • 63
  • 101
TinM
  • 51
  • 8
  • 2
    What to you mean by `Without writing into it` ? – Nathan Jan 17 '20 at 11:12
  • I wasn't clear there, sorry. I don't want to answer these questions in the console application, I would like to answer these before, like before "settings". – TinM Jan 17 '20 at 11:14
  • 1
    There's a wide range of storage available from a simple structured Xml or Json file to a real database of any flavor. – Filburt Jan 17 '20 at 11:15
  • So you mean pass arguments to .exe? Well first you application should be ready to accept and process those arguments. If it is not doing it right now, then you have to write some code and recompile it. – Alexander Goldabin Jan 17 '20 at 11:15
  • 1
    I think you mean command line arguments - see https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/command-line-arguments. – Polyfun Jan 17 '20 at 11:16
  • I can compile my program, it's running well without any problem. Yes, I want to pass these arguments to .exe, I am gonna look after it. – TinM Jan 17 '20 at 11:17
  • 1
    @Polyfun No, not command line arguments - just a configurable data source of items to run thru. – Filburt Jan 17 '20 at 11:17
  • 1
    look into application settings, data persistance, registry.. – TaW Jan 17 '20 at 11:18
  • @Filburt, the OP's comment "I want to pass these arguments to .exe" clarifies the original requirement--sounds even more like command line arguments now. – Polyfun Jan 17 '20 at 11:28
  • @Polyfun You'll have to tribute this to a lack of understanding from the OP - the use case he describes clearly indicates a kind of data driven application with a persisted set of items to present to the user and prompt input on those. – Filburt Jan 17 '20 at 11:30
  • 1
    @TinM Since you are facing a hard time in getting an answer (you deleted your follow up question) I'd suggest you take the time to provide a little more context of what you are trying to achieve on a higher level rater than asking directly about parameters and serialization because answers will depend very much on user experience you are trying to create. Maybe try editing your post here and clarify instead of starting over again - it will help new people seeing your post understand. – Filburt Jan 17 '20 at 13:38

1 Answers1

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();
    //....
    

    }

Michael Gabbay
  • 413
  • 1
  • 4
  • 20
  • 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 Jan 17 '20 at 11:33
  • @TinM [Serializing a list to JSON](https://stackoverflow.com/q/9110724/205233) can give you some clues. – Filburt Jan 17 '20 at 11:35