For Each: Unable to cast object of type 'System.Char' to type 'System.String' error

Hello All,

I am looping through the excel files in the folder , but getting the error unable to cast object of type ‘System.Char’ to type ‘System.String’ as shown below,

Inputfilepath and folder paths are string variables.

Can I please know how resolve this error.



May i know why are you looping through a string?

a loop on string will give characters in loop and not string

Remove .ToString from the For each list items


Hi @sushmitha.e

In For Each pass only Input_file_path and Argument Type will be System.String. Do not pass .ToString in For Each.


Hi @sushmitha.e

No need to use For each loop you are taking one file from folder based on your syntax.You can directly use that variable.


Yep @lrtetala , if we want to read all the files not only one file ,

Directory.GetFiles(FolderPath,“*.xlsx”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).ToList(0)

In the above path, what should we provide instead of List(0) , to read all the files.


Directory.GetFiles(FolderPath, "*.xlsx").OrderByDescending(Function(d) New FileInfo(d).CreationTime).ToArray()

Input_file_path variable is of datatype System.String[ ]



First thing, remove the .ToString and second change the Argument type to string array.

Ashok :slight_smile:

Yes @ashokkarale , I have changed thanks for replying.

Yes @vrdabberu , I have changed. Thanks for replying.

Yep @Anil_G , removed them. Thanks for replying.

1 Like

Perfect @sushmitha.e the topic can be closed.


If Input_file_path is string, you don’t need .ToString on the end of it.

You also don’t need to create extra variables (FolderPath, Input_file_path). You also don’t need .ToList on the end of GetFiles, it’s already an array.

Drop in a new For Each and put this directly into “list of items”:

Directory.GetFiles("D:\SUSHMA....","*.xlsx").OrderByDescending(Function (d) New FileInfo(d).CreationTime)