CS-Script 3.27.0

Accessing Script Config File Tutorial   

Step-by-step tutorial 

The following tutorial shows how to access script application settings stored in the <scriptName>.config or any other compatible configuration file.

create and execute the script that allows users to print any text string to the default printer with print preview. This is an example of multiple scripts application. 

  1. By using any text editor (eg. Notepad.exe) create file AppSettings.cs that contains the following code:

    using System;
    using System.Configuration;
    using System.Xml;
    using System.Collections;
    using System.Collections.Specialized;
    using CSScriptLibrary;
    using csscript;

    class Script
    {
        static public void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Script file: " + CSSEnvironment.ScriptFile);
                Console.WriteLine("Config file: " + CSSEnvironment.ConfigFile);
                Console.WriteLine("---------");

                IDictionary values = (IDictionary)CSSEnvironment.GetConfig("testSection");
                Console.WriteLine("testSection.value0 = " + (string)values["value0"]);

                Console.WriteLine("greeting0 = " + CSSEnvironment.AppSettings["greeting0"]);

                NameValueCollection values1 = (NameValueCollection)CSSEnvironment.GetConfig("appSettings");
                Console.WriteLine("greeting1 = " + values1["greeting1"]);

                CSSEnvironment.ConfigFile = CSSEnvironment.ConfigFile+".bak";
                Console.WriteLine("Config file: " + CSSEnvironment.ConfigFile);
                Console.WriteLine("---------");
                Console.WriteLine("greeting0 = " + CSSEnvironment.AppSettings["greeting0"]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }

    Click here to obtain full listing of AppSettings.cs
  2. Create the configuration file AppSettings.cs that contains the following data:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="testSection" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral,PublicKeyToken=b77a5c561934e089">
        <

    Click here to obtain full listing of AppSettings.cs.config
  3. Make a copy of the AppSettings.cs.config and name it AppSettings.cs.config.bak. In the AppSettings.cs.config.bak change a few values (eg. as this AppSettings.cs.config.bak).
  4. Open command prompt. Make sure current directory is the directory where AppSettings.cs located. 
  5. Execute the following command in command-prompt:
    cscs AppSettings.cs

Output 

The script will produce the following output:


Code discussion 

The code from this tutorial demonstrates the use of the CSSEnvironment class. 

The first section of the application output contains names of the script file being executed and default script configuration file containing the application settings.


...
Console.WriteLine("Script file: " + CSSEnvironment.ScriptFile);
Console.WriteLine("Config file: " + CSSEnvironment.ConfigFile);
...

The second section of the output contains values of the greeting0 and greeting1 fields from the AppSettings section and field value0 from the testSection section. The code associated with this output demonstrates various techniques of accessing the configuration file fields.
...
IDictionary values = (IDictionary)CSSEnvironment.GetConfig("testSection");
Console.WriteLine("testSection.value0 = " + (string)values["value0"]);

Console.WriteLine("greeting0 = " + CSSEnvironment.AppSettings["greeting0"]);

NameValueCollection values1 = (NameValueCollection)CSSEnvironment.GetConfig("appSettings");
Console.WriteLine("greeting1 = " + values1["greeting1"]);
...

The third section of the output contains value of the greeting0 field from custom script configuration file.  The code associated with this section also demonstrates how to instruct the CS-Script engine to use non-default (custom)script configuration file. 

...
CSSEnvironment.ConfigFile = CSSEnvironment.ConfigFile+".bak";
...
Console.WriteLine("greeting0 = " + CSSEnvironment.AppSettings["greeting0"]);
...

See Also

CS-Script tutorialsCS-Script Runtume Environment  | CS-Script application configuration