Pass a JSON string as a command line argument

后端 未结 6 1756
一个人的身影
一个人的身影 2021-01-04 18:19

I am trying to pass a json string to a C#-Program using Commandline.

The JSON-String looks like this:

{
    \"config\": {
        \"script\": {
             


        
6条回答
  •  猫巷女王i
    2021-01-04 18:55

    Try to save the JSON object into a file, and pass the file as the argument to your application.

    @Wildcard27 : This is an actual use case in order to create Windows Tasks which was used for the faculty degree app. The JSON was just a simple serialization of a DTO that I was using.

    When you serialize the JSON, just save it into a blank file, giving it a proper name so that is unique.

        private string CreateTaskConfigurationFile(string taskName, EquipmentEventExtended eventData, string host)
            {
                List changes = new List
                {
                    new Change(MailConstants.EventName,eventData.EventName),
                    new Change(MailConstants.Deadline, eventData.DateTo.Value.ToShortDateString()),
                    new Change(MailConstants.EventDetails, eventData.EventDetails),
                    new Change(MailConstants.Link,$"{host}/Inventory/Details/{eventData.InventoryId}")
                };
    
                MailTaskModel mtm = new MailTaskModel
                {
                    Body = MailConstants.UpdateTemplate(MailConstants.TaskMailTemplate, changes),
                    Subject = "[Reminder] Upcoming Event needs your attention",
                    ToAddress = "abcdef@gmail.com",
                    IsHtml = true
                };
                var fileName = string.Format(@"E:\{0}.json", taskName);
                using (StreamWriter file = File.CreateText(fileName))
                {
                    JsonSerializer js = new JsonSerializer();
                    js.Serialize(file, mtm);
                }
                return fileName;
            }
    

    Then you provide the file path as an argument to the console application:

    static void Main(string[] args)
            {
                var configFilePath = args[0];
                var mailConfig = LoadConfigurationFile(configFilePath);
                MailManager manager = new MailManager(mailConfig.ToAddress, mailConfig.FromAddress,mailConfig.Subject, mailConfig.Body,mailConfig.IsHtml);
                manager.SendMail();
            }
            private static MailTaskModel LoadConfigurationFile(string configurationFilePath)
            {
                MailTaskModel mailConfig;
                using(var sr = new StreamReader(configurationFilePath))
                {
                    string json = sr.ReadToEnd();
                    mailConfig = JsonConvert.DeserializeObject(json);
                }
                return mailConfig;
            }
    

    You can then use something like

    ConsoleApplication.exe -yourFilePath
    

    I've removed noisy check-ups for nulls and all that so that it's more clear.

提交回复
热议问题