Using System.IO in Net CF

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
I want to find out what files are in a directory. When I use:
HTML:
strTemp = System.IO.Directory.GetFiles("\MyFolder")
it does not cause an exception, it just returns 'System.String[]'.

I've tried with and without final \ (as in System.IO.Directory.GetFiles("\MyFolder\") and with and without .ToString.

I've tried MSDNand etc. but nothing works.

What am I doing wrong? Thank you.
 
This isnt a CF problem. Try using Option Strict On and see what it tells you :)
 
No change, no error message. It successfully copies a file from one folder to another using System.IO.File.Copy(mstrFileToCopyFrom, mstrFileToCopyToAsFile, mblnOverwriteFile), but this doesn't seem to use wildcards, so I have to get all filenames in the directory. Square 1 again!
 
Using Option Strict On, if I type :-

VB.NET:
Dim strTemp As String = System.IO.Directory.GetFiles("c:\")

it gives me the following error:-

VB.NET:
Value of type '1-dimensional array of String' cannot be converted to 'String'

Note that is says type '1-dimensional array' - because that's what GetFiles returns :D So you can either use:-

VB.NET:
Dim strTemp() As String = System.IO.Directory.GetFiles("c:\")

or something like:-

VB.NET:
        For Each ReturnedFilename As String In System.IO.Directory.GetFiles("c:\")
            MessageBox.Show(ReturnedFilename)
        Next
 
Dim strTemp() As String = System.IO.Directory.GetFiles("c:\") still gives System.String, but
For Each ReturnedFilename As String In System.IO.Directory.GetFiles("c:\")
MessageBox.Show(ReturnedFilename)
Next

works just fine, with and without final "\". Thank you.
 
Dim strTemp() As String = System.IO.Directory.GetFiles("c:\") still gives System.String

It will, because it's an array. You'll have to loop through the array, rather than just use strTemp - ie use strTemp(0), strTemp(1) etc etc
 
Thank you! I had not spotted the () after the Dim strTemp. I am still trying to get used to VB 2005 and all its peculiarities, after VB6.
 
Back
Top