Have you ever thought of WriteLine's TextWriter

UiPath has various input-output activities that help us build attended automation where the bot takes input and shows output to users in various forms.

One of the activities that output information to the console is WriteLine.

We would have used this many times by now for printing some text in the UiPath Studio output console.​​​​​​​

UiPath WriteLine Text

This is a usual way, but anytime have you checked what's the use of the TextWriter property in WriteLine?

UiPath WriteLine TextWriter

As we know, UiPath is built on .NET. So, let's discuss this in .NET way for a while.

WriteLine in .NET

Please go through what is .NET class & functions which will help you understand the below explanation better.

WriteLine is a dotnet function/method defined under three classes, 

  1. System.Console.WriteLine
  2. System.IO.TextWriter.WriteLine
  3. System.IO.StreamWriter.WriteLine

System.Console.WriteLine is a function which helps in outputting text to the console (By default this is what we are using in UiPath WriteLine)

Now let's say you want to change the output from the console to something else like a file or blob may be...

This is when the other two classes come into the picture.

About TextWriter

In WriteLine since we are only passing value to the Text field leaving the TextWriter property empty, it assumes the default source is the console and hence printing output in the console.

Let's mention some sources this time in TextWriter. Considering a text file is the place to write the text.​​​​​​​​​​​​

Let's try to write hello world in a text file source using Write Line (which is already created).

TextWriter is an abstract class, So we are creating an object from its derived (child) class StreamWriter.

StreamWriter with file path will create a buffer in memory so that we can perform a write operation to that file.

The above tech jargons are purely related to .NET, and knowing that helps. If it is confusing just skip to just understand the usage of WriteLine.

UiPath WriteLine TextWriter

The automation executed well & it was not printing in the console panel.

But still, the text file source is empty :(

So there is a catch here to be taken care of. 

As we discussed, StreamWriter is going to just create a memory buffer for performing the file operation. But we still need to close it which is why it is not getting stored in the file.

To make it work, we need to just Flush it.

Here you go with the text file source having the output text printed as Hello World... :)

So this is all about it. Check the detailed docs of UiPath WriteLine.

Please feel free to add your points & queries over comments.

Reference - https://github.com/nmnithinkrishna/UiPath-WriteLine-TextWriter

No comments:

Post a Comment

Let me hear you 🙌✨

2023 Copyright : nmnithinkrishna.me