The C# action is currently only available in beta. Similar to, for example, the VB.NET and Python actions in Foxtrot, you are also able to write, compile, and execute your own custom C# code. CSharp (C#) is a multi-paradigm, object-oriented programming language that can be very powerful for automation. In Foxtrot, you can make a C# action that either reads the code that you have in a text file or the code that you write directly in the action form in Foxtrot. When you run C# actions in Foxtrot, whether it is the file or code method, Foxtrot compile the code as an .exe file at runtime and execute the program. There are no requirements to install any additional software in order to be able to run the C# action.
In this article, we will take a look at how you use the C# action in Foxtrot. We will not in details look at how you write C# code. If you have zero experience with C#, we recommend that you study some basics before using it in Foxtrot. You can find some great resources online. Here are some that we find great:
Getting started
To create a new C# action, go to the Advanced section in the Action List and select the action called "C#". This is how it looks by default.
The method
As already mentioned, you can choose whether you would like to run "Code" or a "File":
- Code = Will compile the content of the action and run it.
- File = Will read and compile the content of the specified file and run it.
There are pros and cons to both approaches. With the "Code" approach, you can make use of Foxtrot variables, tokens, list lookups, etc., which is not possible with the "File". On the other hand, working with a big chunk of code in the Foxtrot window is not that easy, so for bigger pieces of code, it might be easier to have in separate files. We will go through both of the methods but mainly focus on the "Code" method.
Timeout
Foxtrot will at any time wait for the code to complete before proceeding with the Foxtrot project. This is very handy as the execution of the code might vary. But, what if your code will never end due a mistake in the code causing, for example, an infinite loop? The timeout is a feature added to make sure you do not risk running some infinite code that never ends. You can set the timeout to anything you would like, the default value is 30 seconds. Keep in mind that your code will be stopped by force if the timeout is reached.
Code
If you select the "Code" method, here is where you will write your C# code to execute. With the "Code", Foxtrot will basically take the code you write and compile it in a temporary .exe file, execute the program, and delete it again. The great thing about this approach is that it supports using Foxtrot variables, tokens, list lookups, etc.
File
If you select the "File" method instead, you will have to simply specify the path to the file you wish to execute. Foxtrot will execute the file "as-is", therefore, this approach will NOT support the use of any Foxtrot variables, tokens, list lookups, etc. in the code itself.
Save to
Lastly, you can decide whether you wish to save the result (output) of the code to a Foxtrot variable. We definitely recommend that you always do this, however, it is not mandatory. The output will be:
- The output of any "Console.WriteLine" statements (also "Console.Write")
So, if you do not have any "Console.WriteLine" statements in your code and there are no errors, the output will be empty. Here is an example. Make sure to create a Foxtrot variable called "Output" and write one line of code like this:
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello Foxtrot World");
}
}
}
If everything is setup correctly, you should get an output in your "Output" variable with the value from the "Console.WriteLine" call.
Now, let us try to force an error! Simply "forget" to surround the value of the Console.WriteLine statement with '' like this:
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine(Hello Foxtrot World);
}
}
}
This code is not correct, so we should not expect it to work. Now, as opposed to how the Python action in Foxtrot works, you will actually get the error message like a traditional error in Foxtrot if the code can not be compiled successfully. In this case, you should receive the following error message:
The error message specifically explains where it failed to compile, in this case at line number 5, and it mentions the error number, in this case "CS1026". Now, the error number might not mean much to you, but it is included on purpose as it is sometimes easy to find the answer to the problem using the error number to research for a solution. For example, if you look up the error number on Microsoft's website, this is what you get, which is quite nice in terms of quickly finding the answer.
That is the very basics of the C# action in Foxtrot. It works exactly the same if using the "File" method. Just as a quick example, let us open Notepad and create a new file containing the same line of code. Normally, you would use Visual Studio or a similar application, but we use Notepad here just for the sake of simplicity.
You can save the file as a ".cs" or ".txt" file. Now, close the Notepad application and let us try to run this file from Foxtrot:
You should get exactly the same result as before in your "Output" variable:
How to set Foxtrot variables
The C# action comes with a very nice feature if you need to set multiple variables in Foxtrot with one C# action. As we have explored so far, the information saved to the output variable is all the "print" statements or an error message. But, in many cases you actually need to set multiple different Foxtrot variables from one C# action. You can do this using the special Foxtrot function called "RPAEngine.SetVar". The syntax is:
RPAEngine.SetVar("Foxtrot_Variable_Name", CS_Value)
Here is an example of how this works:
So, as you can see, we can set multiple set the value of multiple variables in Foxtrot using one C# action, which is very useful in many cases. Typically, you will use the "Console.WriteLine" statements to log certain events throughout your code so that you can evaluate whether your code was successful after execution, while you use the "RPAEngine.SetVar" feature to set the value of specific variables.
How to load dlls
Consider the code below:
using System.Windows.Forms;
namespace Program
{
class Program
{
static void Main()
{
MessageBox.Show("Hello Foxtrot World");
}
}
}
The purpose of the code is to display a standard windows message box with the text "Hello Foxtrot World". You would probably expect this code to work, however, it won't work out of the box. Running a C# action with this code will result in the following error:
The cause of this problem is the lack of access or awareness of the necessary dll by Foxtrot. Since Foxtrot is simply compiling the code at runtime without knowledge of any (if any) existing setup as opposed to, for example, a Visual Studio setup. So, in this case, you need to first load the required dll in order to be able to import the "System.Windows.Forms". For this purpose, a special Foxtrot command was developed similarly to the "RPAEngine.SetVar" function. The special Foxtrot function is called "RPAEngine.LoadDLL". The syntax is:
RPAEngine.LoadDLL("Path")
Now, you do not necessarily need to specify the full path if the dll is in the standard Windows path, but it is possible to include the full path to the desired dll file. To solve this specific problem, we need to load the “System.Windows.Forms.dll”. So, if we add this line to the code:
RPAEngine.LoadDLL("System.Windows.Forms.dll")
The full code should be:
RPAEngine.LoadDLL("System.Windows.Forms.dll")
using System.Windows.Forms;
namespace Program
{
class Program
{
static void Main()
{
MessageBox.Show("Hello Foxtrot World");
}
}
}
And instead of giving an error message, you should now see a very simple message box with the specified text:
That's it! Now you should know the basics of how to get started using the C# action in Foxtrot to write your own code to do amazing things as part of your Foxtrot automation projects.
Comments
0 comments
Please sign in to leave a comment.