Get The Owner of a File

Description: You have developed an automation that requires the use of files in a shared directory. As your automation is processing files within the shared directory, you may notice that an error occurs or that the automation does not process certain files. Perhaps there has been an issue with the content of the file or perhaps a user had the file open exactly as the automation was processing it. In any event, it’s helpful to both developers and end users to have some idea of what occurred.

Potential Approach: One way to provide additional detail is to log the owner of that file as well as the file name. I will outline here how to capture the file owner in the event that an automation has an issue processing. Ex (If the error returned was because there was an issue with the content saved in the file, you can get the appropriate user and inform them of the issue once you have these file details) This solution will help minimize bot downtime and keep users informed.

Building the solution: This solution has been designed to use C#. IF you are using custom C# code, you will need to add the following packages listed below:

-System.IO.FileSystem.AccessControl
-System.Security.AccessControl
-System.Security.Principal.Windows

Once the necessary packages have been installed, add an invoke activity to your workflow and be sure to change the language to CSharp. You will also need to create a variable to represent “fileName”. Add the variable to the entire workflow as well as the arguments of the invoke code activity.

**It may take some time for the UiPath system to register the added packages (in case you receive any invalid reference messages)

Code:
var fs = System.IO.File.GetAccessControl(fileName);
var sid = fs.GetOwner(typeof(SecurityIdentifier));
output1 = sid.Translate(typeof(NTAccount)).ToString();
Console.WriteLine(output1); //This line outputs the user and their group in the format “Group\user_account”

1 Like