Blue Prism V5.0中如何将文件名字符串数组转换为集合?
解决Blue Prism V5.0中将C#文件路径数组转换为集合的问题
Hey there! I’ve worked with Blue Prism’s code stages quite a bit, so let’s get this sorted for you. The key here is translating the C# string array into a Blue Prism Collection—the platform’s native way to handle tabular data. Here’s a step-by-step solution:
Step 1: Set Up Blue Prism Variables First
Before writing any code, configure your code stage’s inputs and outputs:
- Input: Create a text variable (e.g.,
inputFolderPath) to pass the target folder path. - Output: Create a Collection variable (e.g.,
outputFileList) and define a column in it—something likeFullPathwith a data type of Text. This is where we’ll store each file’s path.
Step 2: C# Code to Convert Array to Blue Prism Collection
Replace your existing code with this snippet in the code stage. It handles fetching files, initializing the collection, and populating it:
using System.IO; // Make sure this namespace is included for Directory and Path classes // Fetch all file paths from the target folder string[] filePaths = Directory.GetFiles(inputFolderPath); // Initialize a new Blue Prism Collection Collection fileCollection = new Collection(); // Add the column we defined in Blue Prism (match the column name exactly!) fileCollection.AddColumn("FullPath", DataType.Text); // Loop through each file path and add it to the collection foreach (string filePath in filePaths) { fileCollection.AddRow(); // Create a new empty row // Set the value of the "FullPath" column for the new row fileCollection.SetValue("FullPath", fileCollection.RowCount, filePath); } // Assign the populated collection to our output variable outputFileList = fileCollection;
Optional: Get Only Filenames (Not Full Paths)
If you don’t need the full file path and just want the filename (e.g., document.pdf), modify the line inside the loop to use Path.GetFileName():
fileCollection.SetValue("FullPath", fileCollection.RowCount, Path.GetFileName(filePath));
Key Notes to Avoid Issues
- Column Name Match: Ensure the column name in
AddColumn()andSetValue()exactly matches the column name you defined in the Blue Prism output collection. Mismatches will cause errors. - Permissions: The account running the Blue Prism service needs read access to the target folder—otherwise, you’ll get a permission denied exception. Add a
try-catchblock if you want to handle this gracefully:try { // All the core code above goes here } catch (UnauthorizedAccessException ex) { // Throw an error Blue Prism can recognize and handle throw new Exception("Permission denied when accessing folder: " + ex.Message); } - Version Compatibility: This logic works seamlessly for Blue Prism V5.0—later versions use nearly identical collection handling, so you won’t run into breaking changes here.
内容的提问来源于stack exchange,提问作者KHV
相关产品推荐
相关产品推荐

