The script engine is a small runtime environment and as such
some characteristics, which may be accessed/analysed at runtime. The
CSScriptLibrary contains class CSSEnvironment,
which provides information about the CS-Script runtime environment. Use
class to retrieve information such as:
object containing the data from the script engine
custom configuration file (eg. css_config.xml).
Location of all cache and temporary files (auto-generated by the script
engine) for the script file being executed.
- ScriptFile (obsolete)
The name of the script file being executed.
- PrimaryScriptFile (obsolete)
The name of the primary script file. It is the same as the name of the
script being executed, except for the scripts, which are subject of the
pre/post execution actions. In this case it is the name of the parent
script containing pre/post execution directives.
Because CSSEnvironment interface may vary from version
to version see CSScriptLibrary help in Programming
ScriptFile and PrimaryScriptFile are made obsolete as they may not work
correctly in the hosted and cached scenarios. Use alternative
techniques demonstrated in theReflectScript.cs sample. Including
environment variable 'EntryScript' and AssemblyDescriptionAttribute (of
the script assembly) containing the full path of the script file.
Returns the name of the cache directory for the given script file.
The full name of the script being executed can also be discovered in a
very simple way, which does not require loading any external
Every script being excuted has its assembly stamped with the
AssemblyDescriptionAttribute value of which is set to the full script
More samples on how
obtain the script name from the script can be found in the
ReflectScript sample (<cs-script>\Samples\ReflectScript.cs).
var scriptFile = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), true)
In addition to this the script engine always sets at startup some
environment variables for current process:
- CSScriptRuntime - version of the script engine.
- CSScriptRuntimeLocation - location of the script engine.
- CSScriptDebugging - name and version of the debugger/IDE
This can be useful when you need to know at run time if the execution
is under CS-Script environment and/or IDE. The following code
In addition to the environment variables you can also detect "under
IDE" (e.g. Visual Studio) execution with the compiler symbol CSS_PROJECT:
static public void Main(string args)
string cssRuntime = Environment.GetEnvironmentVariable("CSScriptRuntime");
if (cssRuntime != null)
Console.WriteLine("Running under CS-Script v" + cssRuntime.ToString());
string cssDebugging = Environment.GetEnvironmentVariable("CSScriptDebugging");
if (cssDebugging != null)
Console.WriteLine("Debugging under " + cssDebugging.ToString());
Console.WriteLine("Running as stand-alone application");
In script hosting scenarios it can be useful to distinguish
"scripted"code from "static" one.
Console.WriteLine("Script is opened with Visual Studio.");
This can be useful when you need to know at run
time if the methed implemented by the host application is invoked by
the Host or Script routine. CS-Script engine stamps every
compiled script assembly with the AssemblyDescriptionAttribute
custom attribute containing the script file as attribute Description
property. Thus you can always analyse your StackFrame
check if the calling assembly is the script being executed.
To simlify this task CSScriptLibrary
implements Assembly extension method GetScriptName()
|public void SomeHostMethod()
(Assembly.GetCallingAssembly().GetScriptName() != null)
from the script...");
Console.WriteLine("Invoked from the host...");