Over a million developers have joined DZone.

Notepad++ with the C# compiler - you can do it

· IoT Zone

Access the survey results 'State of Industrial Internet Application Development' to learn about latest challenges, trends and opportunities with Industrial IoT, brought to you in partnership with GE Digital.

If you ever decided to to work a bit outside of Visual Studio and configure a plain text editor to work with the C# compiler, you most likely know that it's not so straightforward as it might seem. At first, I thought that it would be a relatively easy task to add a specific compiler reference to Notepad++, but it needed some additionalwork, as Notepad++ has no specific wizard or dialog to directly configure a custom compiler. However, there is a Run box (found in the Run menu) that can help you solve many problems:

Posted Image

There are two ways to use a compiler - either use the ... button and specify the path to the compiler, and then save it, or you could configure the PATH environment variable and use the compiler directly from the command line by calling the executable name.

I am going to start with the second option - set the specific PATH variable and then use csc (the C# compiler) from inside Notepad++. The reason behind this is that sometimes you will want to compile from the console (without an IDE or text editor) and this will make the task a lot easier. Not that you will encounter this scenario very often, but it is always a great plus to know how it can be automated - for example, you might want to have some compilation scripts later.

Before going any further, I tried to see how to set all correct PATH references so that I will make the system console .NET-enabled (excluding the option of running the Visual Studio Command Prompt bundled with the VS release you have). In the Program Files folder (or the host folder where you installed Visual Studio) there is the Visual Studio x.0 folder (replace x with the most recent version of Visual Studio installed on the machine). In the Common7 folder you will find the Tools folder and this is the final destination for now.

Run the command prompt (WinKey+R, cmd) and simply drag the vsvars32.bat file in the console. Then, just press ENTER and the PATH variable will be automatically set so that you can run .NET tools from the command line. Technically, this would be the same procedure as if you would run the Visual Studio Command Prompt, but now you know how this process actually starts.

NOTE: Don't run the BAT file directly from Windows Explorer, as it will have no effect - it only sets the environment variables for the current session.

Once you have the PATH variable set, you need to get the newly-set values to make them permanent, and not only for the current session. Let's do it the simple way - just save the console output for the PATH in a text file. To do this, just type in the console (make sure you are in the folder you want the file to be saved into):

path > mypath.txt

Close the Command Prompt and go to the text file you just saved. Copy everything after PATH=. Go to the System Properties dialog (WinKey+Pause Break) and find the Advanced sytem settings (it's called so in Windows Vista and 7 - not entirely sure about previous versions). In the Advanced tab is the Environment button and this is exactly what's needed.

Click it and you will see that there are two lists of environment variables in the new dialog. The first one is for the current user and the second one is for system-wide environment variables. Select Path from System variables and edit it. Completely replace the existing string with the one you just copied and click OK. To test the new settings, try opening the Command Prompt and simply type csc. The output should look like this:

Posted Image

Awesome! So the compiler can be accesed without explicitly specifying the path. Let's go to Notepad++ and configure it to work with csc. Type a simple C# program in the editor:

using System;namespace ConsoleApplication{class Program{static void Main(string[] args){Console.WriteLine("Sample");Console.ReadLine();}}}

Click on Run > Run.... You will now need to enter the program to be used to compile the file. There are several internal environment variables that you can use the supply the file name to the compiler, since passing the direct path to one file is not exactly what I wanted - after all, I want to re-use the capability. Suppose that you saved your file (and it should be saved before compiling) as C:\Documents\MyFiles\sample.cs. There are a couple of internal variables (specific to Notepad++) that can be used to reference the file you are currently working with:

FULL_CURRENT_PATH - is the full path to file - C:\Documents\MyFiles\sample.cs
CURRENT_DIRECTORY - just the directory where the file is located - C:\Documents\MyFiles
FILE_NAME - the file name only - sample.cs
NAME_PART - the part that only shows the name of the file without the extension - sample
EXT_PART - the file extension - cs

You need to use the full path to compile the file, so here goes the actual command:


Why type the exe extension? Because there is a CSC folder in the system folder, and typing only CSC will result in opening the folder instead of launching the compiler.

The above command will create an executable with the same name as the source file in the user folder.

If you want to specify another folder for the executable to be built in, use the out parameter:

csc.exe /out:"D:\Temporary\myfile.exe" $(FULL_CURRENT_PATH)

Once the command is working you can save it in from the Run dialog for easy re-use.

NOTE: You can customize the command line parameters for the compiler as you want - after all, you might want to compile a library and reference additional assemblies.

The IoT Zone is brought to you in partnership with GE Digital.  Discover how IoT developers are using Predix to disrupt traditional industrial development models.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}