A simple example of using variables in c# to store information.
- Operating System environment variables
- Variables stored in a .env file without additional libraries
This is useful for moving from different environments such as Dev, QA, and Prod. This can be used to store confidential information but it will be in clear text and available with anyone that has machine or file access. Vaults or password managers are highly recommended for credentials and keys. Keep it secret. Keep it safe.
Download or pull the repository and open the solution in Visual Studio. The provided form will display the values of the variables stored in the .env file and the operating system environment variables once entered by the user.
Operating System environment variables Run the program and from the form set or get an environment variable. Targets can be Machine, Process, or User type variables. Experiment with these and see if they will meet your needs. I needed something that was more portable and flexible and thus went with the .env file approach below.
Variables stored in a .env file Create a .env file and populate it with the needed information using a Name=Value format, per line. Or copy and rename the .env-sample file to .env. Run the program, enter the variable name, and view the value. The top section of the .env grouping looks for the .env file inside the program file locations. The botton section of the .env grouping allows to user to select the .env file to be used. Example: C:\Secret\prod.env or C:\Secret\dev.env.
Finding flexible and secure ways to store program information is critical. There are several options available and depending on your needs several might be needed. Here, we looked at two common approaches and hopefully learned something new.
- Microsoft's learn site. Always a good reference to start with.
- Asgarov, Kamran (2024). "Understanding Environment Variables and Configuration Files in .NET C#"
This project is licensed under the MIT License - see the LICENSE.txt file for details
- You can freely modify and reuse.
- The original license must be included with copies of this software.
- Please link back to this repo if you use the source code.