PC Review


Reply
Thread Tools Rate Thread

Convert 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' to '1-dimensional array of String'.

 
 
roidy
Guest
Posts: n/a
 
      15th Jul 2009
Hi, I`m trying to get the files in a directory using:-

Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")

But get the following error:-

Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)'
cannot be converted to '1-dimensional array of String'


I need the files as an array of strings to pass to another function. So my
question is how to convert a read only collection of strings to an array of
strings

Thanks
Rob


 
Reply With Quote
 
 
 
 
roidy
Guest
Posts: n/a
 
      15th Jul 2009
Dim files as string() = Directory.GetFiles("c:\") - this work but I need to
support multiple wildcards eg.

My.Computer.FileSystem.GetFiles("c:\",
FileIO.SearchOption.SearchTopLevelOnly, "*.txt", "*.doc")

It seem as if Directory.GetFiles only supports a single wildcard.

Rob



"roidy" <(E-Mail Removed)> wrote in message
news:IPo7m.810$(E-Mail Removed)2...
> Hi, I`m trying to get the files in a directory using:-
>
> Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")
>
> But get the following error:-
>
> Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of
> String)' cannot be converted to '1-dimensional array of String'
>
>
> I need the files as an array of strings to pass to another function. So my
> question is how to convert a read only collection of strings to an array
> of strings
>
> Thanks
> Rob
>
>

 
Reply With Quote
 
gillardg
Guest
Posts: n/a
 
      15th Jul 2009
hi here is a one minute solution


Dim files As String()
Dim x As Integer = 0
For Each str As String In My.Computer.FileSystem.GetFiles("c:\")
ReDim files(x + 1)
files(x) = str
x = x + 1
Next


but maybe there is a better way to do that

"roidy" <(E-Mail Removed)> a écrit dans le message de groupe de discussion :
IPo7m.810$(E-Mail Removed)2...
> Hi, I`m trying to get the files in a directory using:-
>
> Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")
>
> But get the following error:-
>
> Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of
> String)' cannot be converted to '1-dimensional array of String'
>
>
> I need the files as an array of strings to pass to another function. So my
> question is how to convert a read only collection of strings to an array
> of strings
>
> Thanks
> Rob
>
>

 
Reply With Quote
 
roidy
Guest
Posts: n/a
 
      15th Jul 2009
Thanks, that was the kind of code I`d come up with, I was hoping there was a
more elegant single line solution.

Rob

ps You need ReDim Preserve else the array is wiped every time

"gillardg" <gillardg*remove*@live.be> wrote in message
news:#(E-Mail Removed)...
> hi here is a one minute solution
>
>
> Dim files As String()
> Dim x As Integer = 0
> For Each str As String In My.Computer.FileSystem.GetFiles("c:\")
> ReDim files(x + 1)
> files(x) = str
> x = x + 1
> Next
>
>
> but maybe there is a better way to do that
>
> "roidy" <(E-Mail Removed)> a écrit dans le message de groupe de discussion :
> IPo7m.810$(E-Mail Removed)2...
>> Hi, I`m trying to get the files in a directory using:-
>>
>> Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")
>>
>> But get the following error:-
>>
>> Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of
>> String)' cannot be converted to '1-dimensional array of String'
>>
>>
>> I need the files as an array of strings to pass to another function. So
>> my question is how to convert a read only collection of strings to an
>> array of strings
>>
>> Thanks
>> Rob
>>
>>

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      15th Jul 2009
On 2009-07-15, roidy <(E-Mail Removed)> wrote:
> Hi, I`m trying to get the files in a directory using:-
>
> Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")
>
> But get the following error:-
>
> Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)'
> cannot be converted to '1-dimensional array of String'
>
>
> I need the files as an array of strings to pass to another function. So my
> question is how to convert a read only collection of strings to an array of
> strings
>
> Thanks
> Rob
>
>


Dim files As String() = System.IO.Directory.GetFiles ("c:\")

My suggestion is to learn the framework, and abandon most of the My.Crap
stuff.

--
Tom Shelton
 
Reply With Quote
 
roidy
Guest
Posts: n/a
 
      15th Jul 2009
If you read my 2nd post you`ll see I can`t use System.IO.Directory.Getfiles
because it doesn`t support multiple wildcards.

Rob

"Tom Shelton" <(E-Mail Removed)> wrote in message
news:e$(E-Mail Removed)...
>
> Dim files As String() = System.IO.Directory.GetFiles ("c:\")
>
> My suggestion is to learn the framework, and abandon most of the My.Crap
> stuff.
>
> --
> Tom Shelton


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      15th Jul 2009
On 2009-07-15, roidy <(E-Mail Removed)> wrote:
> If you read my 2nd post you`ll see I can`t use System.IO.Directory.Getfiles
> because it doesn`t support multiple wildcards.
>
> Rob
>
> "Tom Shelton" <(E-Mail Removed)> wrote in message
> news:e$(E-Mail Removed)...
>>
>> Dim files As String() = System.IO.Directory.GetFiles ("c:\")
>>
>> My suggestion is to learn the framework, and abandon most of the My.Crap
>> stuff.
>>
>> --
>> Tom Shelton

>


I can see how my answer could have been read as insulting - but, it was not
meant to be so. I apologize for that.

Still, I would avoid My for this. A simple function along these lines should
suffice:

Function GetFiles(ByVal path As String, ByVal ParamArray wildCards() As String) As String()
If wildCards.Length = 0 Then
Return Directory.GetFiles(path)
End If

Dim files As New List(Of String)
For Each wildCard As String In wildCards
files.AddRange(Directory.GetFiles(path, wildCard))
Next

Return files.ToArray()
End Function

--
Tom Shelton
 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      15th Jul 2009
"roidy" wrote
> my question is how to convert a read only collection of strings to an
> array of strings


Look in the help for ReadOnlyCollection in the "extension methods" section.

Andrew



 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      15th Jul 2009
> Look in the help for ReadOnlyCollection in the "extension methods"
> section.


Well, apart from familiarizing yourself with more of the .NET framework, go
with Tom's suggestion, it is a much better way of going about the original
problem.

Andrew

 
Reply With Quote
 
roidy
Guest
Posts: n/a
 
      16th Jul 2009
Hey it takes more than that to insult me : )

Thanks for the code, can I just ask why should 'My' be avoided? I guess I
was just hoping for an easy way to convert a ReadOnlyCollection(Of String)
to an array of strings.

Rob

"Tom Shelton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I can see how my answer could have been read as insulting - but, it was
> not
> meant to be so. I apologize for that.
>
> Still, I would avoid My for this. A simple function along these lines
> should
> suffice:
>
> Function GetFiles(ByVal path As String, ByVal ParamArray wildCards() As
> String) As String()
> If wildCards.Length = 0 Then
> Return Directory.GetFiles(path)
> End If
>
> Dim files As New List(Of String)
> For Each wildCard As String In wildCards
> files.AddRange(Directory.GetFiles(path, wildCard))
> Next
>
> Return files.ToArray()
> End Function
>
> --
> Tom Shelton


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 String collections to two-dimensional array Zaur Bahramov Microsoft C# .NET 1 23rd Sep 2009 04:28 PM
function to convert string to 1 dimensional array of long =?Utf-8?B?WE1MIG5ld2JpZTogVXJnZW50IHBscyBoZWxwIQ== Microsoft VB .NET 5 28th Feb 2006 03:46 PM
Quickest way to find the string in 1 dimensional string array! Jozef Jarosciak Microsoft C# .NET 7 12th Jul 2005 12:16 PM
Quickest way to find the string in 1 dimensional string array! Jozef Jarosciak Microsoft VB .NET 3 10th Jul 2005 05:19 AM
Value of type 'String' cannot be converted to '1-dimensional array of System.Object' =?Utf-8?B?U2Vhbg==?= Microsoft ASP .NET 2 6th Feb 2004 01:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:18 AM.