Help required in C# Code

Hi Everyone,

I have below C# code which i want to run in UiPath using invoke code activity but the program is not compiling and giving errors. Any help would be great.
This program is to merge files.

var allCsv = Directory.EnumerateFiles("C:\Users\Documents\UiPath\Files1", "*.csv", SearchOption.TopDirectoryOnly);
string[] header = 
{ 
    File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l)) 
};

 // Get CSV Data
 var mergedData = allCsv.SelectMany(csv => File.ReadLines(csv).SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1)); 

 // skip header of each file
 File.WriteAllLines("C:\Users\Documents\UiPath\test.xlsx", header.Concat(mergedData));

@supermanPunch @Nithinkrishna @ppr can you guys also have a look
Thanks!

Hi,

How about the following?

var allCsv = Directory.EnumerateFiles(@"C:\Users\Documents\UiPath\Files1", "*.csv", SearchOption.TopDirectoryOnly);
string[] header = 
{ 
    File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l)) 
};

 // Get CSV Data
 var mergedData = allCsv.SelectMany(csv => File.ReadLines(csv).SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1)); 

 // skip header of each file
 File.WriteAllLines(@"C:\Users\Documents\UiPath\test.xlsx", header.Concat(mergedData));

Regards,

1 Like

Thanks a lot @Yoichi, the programmed complied now. Can you please tell what did adding a ‘@’ made a difference in the code?

Hi,

Backslash is recognized as escape character in C#. So we need to escape itself if we handle it as literal. Or if we use @ like @"xxx", we can handle backslash as literal even if not escape.
Please also see the following document - section2.

Regards,

Thanks a lot @Yoichi and @ppr

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.