Compiling C# Programs From The Command-Line

Compiling C# Programs From The Command-Line

You don’t need Visual Studio for simple C# projects. Everything you need is included when you install the .NET Framework, except a decent text editor. (I highly recommend Notepad++) To get started, you’ll need to install the .NET Framework, if you haven’t already done so, and set up several environment variables. I first create a user environment variable named DOT_NET_HOME whose value is the absolute path to the location of the csc.exe file for your particular version of .NET. (See Figure 1)

Setting Environment Variables

I then edit the user’s Path environment variable to include the DOT_NET_HOME variable. To reference one environment variable in another, enclose the referenced variable in percent characters like so: %DOT_NET_HOME%   For a detailed tutorial on how to set environment variables in Windows 10 you can watch this video:
Next, create source files with your text editor of choice. For this example I’ll create a class named MyClass with Notepad++ as shown in Figure 2.

Creating MyClass.cs Source File

Save the file and enter the source code for your class. I’ll keep this example simple. Here’s the code:
using System;

public class MyClass {
    public static void Main(){
      Console.WriteLine("Hello World!");
    }
}
Here’s my finished source file:

Figure 3: MyClass.cs Source Code Complete

Open up a console window and navigate to where you saved the source file and use the csc command to compile the file like so:
csc MyClass.cs
If it compiles successfully the result will be a new file named MyClass.exe. Here are the results of compiling and running MyClass:

Compiling and Running MyClass

That’s it!  
Persisting C#.NET Application Settings to Config Files

Persisting C#.NET Application Settings to Config Files

In an earlier blog post I explained how to populate a Windows forms ComboBox with settings stored in an App.config file. It’s easy to read settings from an App.config file and it’s just as easy to save application settings to configuration files as well.
You’ll need to define any settings you wish to retrieve and preserve in the project’s Settings.settings file. These property settings will be automatically transferred to the solution’s App.config file. To save the application’s settings during runtime, simply assign a property its new value and call the Save() method. Here’s an example:
Properties.Settings.Default.MainFormHeight = this.Height;
Properties.Settings.Default.Save();
This code would appear in a method that gets called in response to some application event, perhaps when a user selects a menu item to manually save application settings or when the application exits. When a user first executes an application that utilizes settings, the settings will be read from the application’s configuration file that resides in the working directory. This is the directory from which the application is launched. Application settings are saved in a location unique to each user. For example, an application named MyApp would have its user-specific configuration file saved to a location in the user’s AppData folder. A typical path would be:
C:\Users\username\AppData\Local\MyApp\MyApp.exe_Url_hiunsunuyggzq879b7spbgg6\assembly-version-number
In this way, multiple users of the same application have their individual application settings preserved.
Windows Forms ComboBoxes: Populating Items via App.Config Settings

Windows Forms ComboBoxes: Populating Items via App.Config Settings

A ComboBox control allows you to make a selection from a dropdown list of choices. You can populate a ComboBox from a variety of sources including static initialization in the code or fetching the items dynamically from a database just to name a few. These methods have their advantages and appropriate uses within your code. Populating a ComboBox via property App.config file settings allows you to add or remove items while eliminating the need to recompile the code. Changes to the ComboBox items list will take effect when you restart the program. The following video demonstrates how to create App.config settings and use them to populate the ComboBox items list.    
      Essentially, you’ll need to open the project Settings and create the required setting entry as shown in figure 1.

Figure 1: Entering Settings

In figure 1 – The setting name is WebSites, the type is System.Collections.Specialized.StringCollection, the Scope is set to Application, and the item values are listed as:
  • http://warrenworks.com
  • http://pulpfreepress.com
  • http://photography.warrenworks.com
  • http://msdn.com
Save the Settings.settings file and verify they were properly created in the App.config file as is shown in figure 2.

Figure 2 – Verifying App.config File

Well, that diagram is a wee bit tiny, sorry about that. Here’s the code:
<applicationSettings>
        <ComboBoxTest.Properties.Settings>
            <setting name="Websites" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>http://warrenworks.com</string>
                        <string>http://pulpfreepress.com</string>
                        <string>http://photography.warrenworks.com</string>
                        <string>http://msdn.com</string>
                    </ArrayOfString>
                </value>
            </setting>
        </ComboBoxTest.Properties.Settings>
    </applicationSettings>
Next, you’ll need to use the setting to populate the ComboBox.Items property, which is simply a collection of objects. Here’s the code for a method named “InitializeComboBox()”:
private void InitializeComboBox()
  {
     foreach(string s in Properties.Settings.Default.Websites)
     {
       websiteComboBox.Items.Add(s);
     }
       websiteComboBox.SelectedIndex = 0;
       websiteComboBox.SelectedIndexChanged += ComboBoxIndexChangedEventHandler;
   }

You’ll notice above that in the foreach statement you use the Properties.Settings.Default property to access application settings. In this case we’re interested in the Websites setting, which provides us a list of strings. The last two lines in the method 1) set the ComboBox item to display when the control initially renders, and 2) wires up the event handler named ComboBoxIndexChangedEventHander, which is assigned to the websiteComboBox.SelectedIndexChanged event. Once the InitializeComboBox() method is complete, it needs to be added to the form’s constructor. You can download the Visual Studio project for this example here: http://pulpfreepress.com/VideoVSProjects/ComboBoxTest.zip