views:

299

answers:

2

I am creating an array of string objects in PowerShell which needs to be passed into an Xceed zip library method which expects a string[], but I get an error everytime. It makes me wonder if the PowerShell array is something other than a .NET array. Here is some code:

$string_list = @()
foreach($f in $file_list)
{
 $string_list += $f.FullName
}
[Xceed.Zip.QuickZip]::Zip("C:\new.zip", $true, $false, $false, $string_list)

The error I get says "An error occurred while adding files to the zip file." If I hard code in values like this it works:

[Xceed.Zip.QuickZip]::Zip("C:\new.zip", $true, $false, $false, "test.txt", "test2.txt", "test3.txt")

Can someone help me figure this out? I can't understand what the difference would be...

EDIT: I have tested and confirmed that my $string_list array is composed of System.String objects

+10  A: 

When you specify:

$string_list = @()

You have given PowerShell no type info so it creates an array of System.Object which is an array that can hold any object:

PS> ,$string_list | Get-Member

   TypeName: System.Object[]

Try specifying a specific array type (string array) like so:

PS> [string[]]$string_list = @()
PS> ,$string_list | Get-Member


   TypeName: System.String[]
Keith Hill
Yes! I could kiss you (not in a gay way) (not that there's anything wrong with that)
JimDaniel
A: 

More info on PowerShell arrays: pwrshell.net

juFo