Home .NET C# console for executing simple “scripts”

C# console for executing simple “scripts”

by admin

Have you ever done a mass renaming of files, or any other seemingly trivial but routine task? Never created bat’nicks, but know C#?
Being in a similar situation, it took 15 minutes to create a fairly simple application that looks like a console, but that understands C#.
C# console for executing simple "scripts"
The application works on the principle of memorizing code, and executing the resulting script. It supports executing the scriptfrom a file that appears in the directory with the application when it is first executed.
The basic commandsthat are required to create a scriptthrough the console and run it have been added :
!show -shows the whole saved script
!del – deletes the last lineof the script
!clear – erases the script
!run – runs the script and saves its contents to the file "script.cs"
!runsc – execute script from file "script.cs"(in caseif you edit the file with text editor)
Source Program.cs (the entire application):

using System ;
using System.CodeDom.Compiler ;
using System.Collections.Generic ;
using System.IO ;
using System.Reflection ;
using System.Text ;
using Microsoft.CSharp ;
namespace SharpConsole
{
class Program
{
const string header = @"
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Collections.Generic;
namespace CScript
{
public class Script
{
public staticvoidScriptMethod()
{
"
;
const string footer = @"
}
}
}"
;
static void Main ( string [ ] args )
{
Console. Title = "SharpConsole" ;
Console. WriteLine ( "Welcome to SharpConsole v0.1. Type !help for help." ) ;
List < string > code = new List < string > ( ) ;
while ( true )
{
Console. Write ( "csharp> " ) ;
string line = Console. ReadLine ( ) ;
switch ( line )
{
case "!run" :
{
string program = FormatSources ( code ) ;
File. WriteAllText ( "script.cs" , program ) ;
ExecuteScript ( program ) ;
}
break ;
case "!show" :
{
Console. WriteLine ( ) ;
Console. WriteLine ( FormatSources ( code ) ) ;
Console. WriteLine ( ) ;
}
break ;
case "!del" :
code. RemoveAt ( code. Count 1 ) ;
break ;
case "!clear" :
{
code = new List < string > ( ) ;
Console. WriteLine ( "Done." ) ;
}
break ;
case "!runsc" :
{
string script = File. ReadAllText ( "script.cs" ) ;
ExecuteScript ( script ) ;
}
break ;
case "!help" :
{
string [ ] commands = new string [ ]
{
"1. Type your code and it will be added in script" ,
"2. !show will shows your code" ,
"3. !del remove last line from your code" ,
"4. !clear remove all lines from your code" ,
"5. !run will run script inmemory and writes it inscript.cs" ,
"6.!runscwill run script.cs" ,
"Have fun! :)" ,
} ;
foreach ( var str in commands )
Console. WriteLine ( str ) ;
}
break ;
default :
{
code. Add ( " " + line ) ;
}
break ;
}
}
}
private static void ExecuteScript ( string program )
{
var CSHarpProvider = CSharpCodeProvider. CreateProvider ( "CSharp" ) ;
CompilerParameterscompilerParams = new CompilerParameters ( )
{
GenerateExecutable = false ,
GenerateInMemory = true ,
} ;
compilerParams. ReferencedAssemblies AddRange ( new string [ ]
{
"System.dll" ,
"System.Core.dll" ,
"System.Net.dll" ,
} ) ;
var compilerResult = CSHarpProvider. CompileAssemblyFromSource ( compilerParams, program ) ;
if ( compilerResult. Errors Count == 0 )
{
Console. WriteLine ( "Executing…" ) ;
try
{
compilerResult. CompiledAssembly GetType ( "CScript.Script" ) GetMethod ( "ScriptMethod" ) Invoke ( null , null ) ;
Console. WriteLine ( "Done." ) ;
}
catch ( Exception e )
{
Console. WriteLine ( e. InnerException Message + "rn" + e. InnerException StackTrace ) ;
}
}
else
{
foreach ( var oline in compilerResult. Output )
Console. WriteLine ( oline ) ;
}
}
private static string FormatSources ( List < string > code )
{
string program = header ;
StringBuildersb = new StringBuilder ( program ) ;
foreach ( var sc in code )
{
sb. AppendLine ( sc ) ;
}
sb. AppendLine ( footer ) ;
return sb. ToString ( ) ;
}
}
}

By default, when the script runs, 3 references are connected to the assembly :

compilerParams. ReferencedAssemblies AddRange ( new string [ ]
{
"System.dll" ,
"System.Core.dll" ,
"System.Net.dll" ,
} ) ;

But you can add others for your needs.
VS Project: download
I hope this will be helpful to someone! I will be happy to answer questions.

You may also like