CS-Script 2.7.0

Using Resources

In the majority of the runtime scripting scenarios there is no need to use resources. However in some cases it cannot be avoided. Th CS-Script supports special directive for loading compiled resource file. Thus you can specify directly in script code what resource file should be loaded at execution time:

//css_resource <file>;

file - name of the resource file. The resource file is a compiler managed resource file (etc. .resource compiled from .resx)

The shorter alias directive //css_res can be used in place of the //css_resource.

Note:

The <file> parameter should contain file name only (no path part, neither relative nor absolute, is allowed).

File location

The resource file to be loaded must be from one of the following locations (the order indicates the file search priority):


The following code loads Scripting.Form1.resources resource file at execution time thus code in the Form1 implementation can access any resources from this file.

//css_res Scripting.Form1.resources;
using System;
using System.Windows.Forms;

class Test 

    static public void Main( string [] args)
    {
        Application.Run(new Form1());
    }
}

public class Form1 : System.Windows.Forms.Form
{
    ....
}

Of course a managed resource file represents some value of it's own but also it is "must have" runtime component of some types for the managed applications. 

For example XAML applications (.NET3.0) retrieve compiled XAML file (BAML) from the application resources. This can be changed in the final release of the .NET3.0 but currently all standard XAML application created with WPF relay on use of managed resources. 

Another example is a WinForm application with ActiveX controls. Technically speaking you do not need to have managed resources in you application for this. However if your script application is created with the Visual Studio wizard it will always use managed resources to initialise ActiveX control at startup. It is possible to remove such initialization from the InitializeComponent() and run the code as a script but you may want to preserve InitializeComponent() from any modifications (as it can break a form designer). In this case you script will need to load resources. Thus if your Visual Studio project produces obj\Debug\MyApp.MyForm.resource file it needs to be included in your script.

//css_res MyApp.MyForm.resources;
using System;
using System.Windows.Forms;

namspace MyApp 
{
    class MyForm 
    { 
        .....


The interesting things is that it actually does not matter what resource file is used, ActiveX will be initialized successfully with any valid resource file. That is why for all scripts with ActiveX controls you can specify the same resource file. However it has to be appropriately named. This can be automated by running the res.cs script as a pre-execution script. It is capable of producing appropriate 'dummy' .resource file. The code below is a fragment of the calendar.cs script which uses the Microsoft Calendar ActiveX control (full listing of the calendar.cs can be found here):

//css_pre com(/ax, MSCAL.Calendar, AxInterop.MSACAL.dll);
//css_ref AxInterop.MSACAL.dll;

//css_pre res(dummy, Scripting.Form1.resources);
//css_res Scripting.Form1.resources;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Scripting
{
    public class Form1 : System.Windows.Forms.Form
    {
        ......

The first two lines produce a managed wrapper for MS Calendar ActiveX and instruct CS-Script to referenced produced wrapper assembly. The next two lines produce a managed resource file Scripting.Form1.resources and indicate that the CS-Script engine needs to load this resource file at execution time.

The calendar.cs script can be run as a script or open in Visual Studio and manipulated through the FormDesigner without any code changes. All this because resources are prepared and associated with the script through "//css_res" and "//css_pre res" directives.

Note that calendar.cs  sample can work only if MS Office is installed.

 
If it is required to generate not dummy but a real resources file you need to specify file name of the resource to be embedded as the first parameter of the res (res.cs) script. The following scripts at startup generates resource file script.resources containing binary file csws.exe and embedds the resource file into the script assembly:

//css_pre res(%CSSCRIPT_DIR%\csws.exe, script.resources); 
//css_res script.resources;                              
 

using System;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;

class Script
{
    static public void Main(string[] args)
    {
        var data = new ResourceManager("script", Assembly.GetExecutingAssembly())
                            .GetObject("csws.exe");
                           
        File.WriteAllBytes("csws.exe", (byte[])data);
    }
}

See Also

Script Library | Using COM (Tutorial)