Opening file with no file name

Phil_Pearce

Member
Joined
Dec 30, 2009
Messages
6
Programming Experience
Beginner
Hi,

I am using the StreamReader to open a .csv file and read it into a datatable but I would like to know how to use the StreamReader to open a file without specifying the file name in a specified directory. There will only ever be one file in this directory.

I have tried using GetFile to look in the folder and return the name but that does not seem to want to work.

Any help would be much appreciated.

Phil
 
It doesn't matter that there is only one file in the folder...the name would still have to be specified or StreamReader would not know what you wanted it to open (or, without the folder name, where it is)
 
That's what I thought. Do you know the easiest way to search the folder and return the file name so that can then be used in the streamreader?
 
So
VB.NET:
System.IO.Directory.GetFiles("YourDirectoryPath")(0I)
doesn't work for you?

I have used

VB.NET:
Dim FindSettfiles() As String = Directory.GetFiles("M:\Files\Report\", "*.csv")

and then

VB.NET:
Dim SettFilePath As String = "M:\Files\Report\" & FindSettfiles

Dim myReader As New System.IO.StreamReader(SettFilePath)

The second line has an error which says: Operator '&' is not defined for types 'string' and '1 dimensional array of string'.
 
If there's only 1 file in the folder then the array returned from the System.IO.Directory.GetFiles("YourDirectoryPath") is going to be a 1 dimensional array with only 1 element @ index 0, so:System.IO.Directory.GetFiles("YourDirectoryPath")(0I) gets you the element at index 0 for the folder with only 1 file in it.

If Dim FindSettfiles() As String = Directory.GetFiles("M:\Files\Report\", "*.csv") is a string array, then wouldn't it make sense to grab one of the elements in the array and assign that to the rest of the Dim SettFilePath As String = "M:\Files\Report\" instead of the whole array?
VB.NET:
Dim FindSettfiles() As String = Directory.GetFiles("M:\Files\Report\", "*.csv")
im SettFilePath As String = FindSettfiles(0I)

Dim myReader As New System.IO.StreamReader(SettFilePath)
But then again, that's the same thing as:
VB.NET:
Dim myReader As New StreamReader(Directory.GetFiles("M:\Files\Report\", "*.csv")(0I))
 
JuggaloBrotha thats works a treat. I had tried both of those scenarios but neither worked as i did not include the (0I) at the end.

Many thanks
 
Back
Top