Create a C# Code Editor Using WPF

As a developer, we used to write so many simple applications for PoC or research having few lines of code. To do that, it is required to open Visual Studio, create a solution, build it and then delete after testing it. In these scenarios, we can use this application to edit and run the code quickly as in the following screenshot:



Figure 1: Programm

Create a WPF application and name it MyCodeEditor in Visual Studio 2010. Then add AvalonEdit library using NuGet as in the following screenshot. This is having an advanced textbox with built-in support for code formatting\coloring like Visual Studio Editor.



Figuire 2: Studio Editor

Design the UI with an option to select target framework, select output type as dll or exe, button to execute the code written and AvalonEdit control for editing our code and a textbox to show output of C# compiler as in the following screenshot:



Figure 3: Design the UI

In window_loaded event, we used KB318785 to find list of .NET frameworks installed through registry keys.

  1. private void Window_Loaded(object sender, RoutedEventArgs e)   
  2. {  
  3.     AddVersion(@  
  4.     "Software\Microsoft\.NET Framework\Policy\v1.0""1.0 ");  
  5.     AddVersion(@  
  6.     "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322""1.1 ");  
  7.     AddVersion(@  
  8.     "Software\Microsoft\NET Framework Setup\NDP\v2.0.50727""2.0 ");  
  9.     AddVersion(@  
  10.     "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0""3.0 ");  
  11.     AddVersion(@  
  12.     "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5""3.5 ");  
  13.     AddVersion(@  
  14.     "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full""4.0 ");  
  15.     CboTypes.SelectedIndex = 0;  
  16.     OutputCC.Text = "";  
  17.     textEditor.Text = "using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Text;\r\n\r\npublic class Test\r\n{\r\n public static void Main() \r\n {\r\n System.Console.WriteLine(\"Hello, World!\"); \r\n }\r\n}";  
  18.     CboVersions.SelectedIndex = 0;  
  19. }
In AddVersion method, we are checking whether the passed registry path exist or not. Based on that, we are adding it to available .NET Frameworks combobox.

In Button_Click event, we will write code to read C# code entered in AvalonEdit control, save it as a file, pass it to corresponding csc.exe based on selected framework version, read output of csc.exe and pass it to output textbox control as in the following code snippet:
  1. private void Button_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     try   
  4.     {  
  5.         ClearOldData();  
  6.         string code = textEditor.Text;  
  7.         string selectedVersion = CboVersions.Text;  
  8.         File.WriteAllText(@  
  9.         "C:\Test.cs", code);  
  10.         string installPath = GetInstallPath(selectedVersion);  
  11.         if (!string.IsNullOrEmpty(installPath))   
  12.         {  
  13.             Process p = new Process();  
  14.             p.StartInfo.UseShellExecute = false;  
  15.             p.StartInfo.RedirectStandardOutput = true;  
  16.             p.StartInfo.FileName = installPath + "csc.exe";  
  17.             p.StartInfo.CreateNoWindow = true;  
  18.             p.StartInfo.Arguments = @  
  19.             " /target:" + (CboTypes.Text == "dll" ? "library" : "exe") + @  
  20.             " /out:c:\myfile." + CboTypes.Text + @  
  21.             " c:\test.cs";  
  22.             p.Start();  
  23.             string output = p.StandardOutput.ReadToEnd();  
  24.             p.WaitForExit();  
  25.             output = output.Substring(output.IndexOf("reserved") + 9);  
  26.             if (string.IsNullOrEmpty(output.Replace("\r\n""")))   
  27.             {  
  28.                 OutputCC.Text = "Build Successful...";  
  29.                 if (CboTypes.Text != "dll")  
  30.                 {  
  31.                     Process.Start(@  
  32.                     "c:\myfile.exe");  
  33.                     OutputCC.Text += "\r\n File Path:c:\\myfile.exe";  
  34.                 } else   
  35.                 {  
  36.                     OutputCC.Text += "\r\n File Path:c:\\myfile.dll";  
  37.                 }  
  38.   
  39.             } else   
  40.             {  
  41.                 OutputCC.Text = output;  
  42.             }  
  43.         }  
  44.     } catch (Exception ex)  
  45.     {  
  46.   
  47.         OutputCC.Text = ex.Message;  
  48.     }  
  49. }
In few cases, our application can’t pick correct path of csc.exe. To handle it, we added app.config with path hard-coded in it as in he followig code snippet for picking correct csc.exe.
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.     <appSettings>  
  4.         <add key="1.0" value=""/>  
  5.         <add key="1.1" value=""/>  
  6.         <add key="2.0" value=""/>  
  7.         <add key="3.0" value=""/>  
  8.         <add key="3.5" value="C:\Windows\Microsoft.NET\Framework64\v3.5\"/>  
  9.         <add key="4.0" value=""/>  
  10.     </appSettings>  
  11.     <startup>  
  12.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  
  13.     </startup>  
  14. </configuration>
If we select output type as exe, we will generate .EXE, save it as c:\ myfile.exe and run it. If it is dll, then we will generate a .DLL and save it as c:\myfile.dll.

Let’s run the application and enter few lines of code as in the following screenshot:



Figure 4: Run the App

We can further extend this application by providing options like adding reference to external assembly, edit\compile multiple files, intellisense support in AvalonEdit textbox etc. This application will save time in creating and running simple C# applications for our PoC or research activities. I am attaching source code for reference. I hope this article will be helpful for all.

Up Next
    Ebook Download
    View all
    Learn
    View all