Quick guide: Utilize Robin Scripts through C#

v3. Removed redundant Process.Kill(); method calls.

v2. Added functionality that reads all the scripts from a designated folder and runs them one by one.
Also modified execution methods to kill process when they finish in order to avoid possible memory leaks.

Note: This is the first of 3 parts in which I am going to demonstrate how you can use Robin for Robotic Desktop Automation.
In the first part I am showcasing how to call .robin scripts through your C# projects.
In the second I am going to do the same with Powershell scripts.
In the third I am going to showcase how to utilize the developed .exe from Microsoft’s Task Scheduler to create automated workflows.
Stay tuned…

In this guide I am going to demonstrate how to utilize Robin scripts
through a C# console application.

I am going to call some scripts I have created and execute them consecutively.

After compiling a C# application (for example a console application) an .exe file or a .dll file is produced.
We can then distribute these .exe files to anyone we want (colleagues inside our company for instance).
This allows for standardized execution of scripts.

Note: In order for those .exe files to be executed on a different PC than yours, it must have its own Robin installation as well as a copy of the .robin scripts you wish to execute!

I have created a simple C# Console application (.net core).
There I am launching a CMD and passing Robin commands as arguments.

Three methods have been created:
One dedicated to the help command and another one that is called for both Check and Run commands:
image

Another method that retrieves all the .robin files from a designated folder has been added.
This way you don’t have to hard-code your scripts each time.
image

These methods are then called by the main method of the program:
image

As you can see I have created three string variables that hold the commands and one that holds the path to the folder that contains the automation scripts.

For the example I have utilized the Web Extraction script I created for a previous tutorial.

You can check it out here:
Tutorial: Extracting data from a web page and saving it as an excel file

By hitting Run a new CMD instance is activated and the commands are executed:

A .exe has also been generated.
It resides in your Debug of Release folder depending on the state you have marked your project.

Since we are utilizing the CLI other languages could also be used.
C# is my language of choice.


Screenshot(s) of the complete source:


image


Script:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace RobinScriptUtilization
{
class Program
{
static void Main(string[] args)
{
// Robin has three console commands:
// help
string robinHelp = “Robin -h”;
// check script
string robinCheck = “Robin check”;
// run script
string robinRun = “Robin run”;

        // call the help command
        ExecuteHelpCommand(robinHelp);

        // set the path to the folder/repo containing the script(s)
        string ScriptFolderPath = @"C:\Users\dpapadimitriou\Desktop\";
        
        List<string> Scripts = GetRobinScripts(ScriptFolderPath);
        

        foreach (string robinScript in Scripts)
        {
            // execute the Check command
            //ExecuteCommand(robinCheck, robinScript);
            
            // execute the Run command
            ExecuteCommand(robinRun, robinScript);
        }                        
    }
    
    // Method that retrieves all the .robin files from a specified directory
    public static List<string> GetRobinScripts(string folderPath)
    {
        DirectoryInfo directory = new DirectoryInfo(folderPath);
        FileInfo[] Files = directory.GetFiles("*.robin"); //Getting Robin scripts
        List<string> ScriptsList = new List<string>();
        string script;
        foreach (FileInfo file in Files)
        {
            script = @$" {folderPath}\{file.Name}";
            ScriptsList.Add(script);
        }
        return ScriptsList;
    }

     // Method that executes the help command
    public static void ExecuteHelpCommand(string robinCommand)
    {
        Process process = Process.Start("cmd", "/c " + robinCommand);
        process.WaitForExit();
    }

    // Method that either 
    public static void ExecuteCommand(string robinCommand, string script)
    {
        Process process = Process.Start("cmd", "/c " + robinCommand + script);
        process.WaitForExit();
    }
}  

}


Best regards,
J.


James Papadimitriou
Technical Evangelist

1 Like

Very nice indeed! Thank you!
Looking forward very much to the next installment.
Regards,
burque505

2 Likes

This is great! Thanks @jpap

2 Likes

16/12/2019
Updated to v3.
Removed redundant Process.Kill() method calls.


13/12/2019
Updated to v2.
You can now set a folder containing multiple automation scripts and have them executed sequentially.


Best regards,
J.


James Papadimitriou
Technical Evangelist

2 Likes