5

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

2
  • Can you show us the signature of [Xceed.Zip.QuickZip]::Zip(...)? Commented Aug 26, 2009 at 22:12
  • void Zip(String zipFileName, bool replaceExistingFiles, bool recursive, bool preservePaths, String[] filesToZip) Commented Aug 26, 2009 at 22:37

1 Answer 1

16

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[]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.