我有一个问题已经几天了,下面这个脚本的执行导致了异常。
我使用多个C#构造函数来帮助输入大量行到数组中。我的问题可能来自于PowerShell语法,代码在C#中可以正常工作。
我对PowerShell很新,我对我正在做的事情没有信心...
$source = @"
using System.IO;
public class DesktopIni
{
public const string Shell32 = @"%SystemRoot%\system32\shell32.dll";
// Ctor without folder, with localizedRes
public DesktopIni(string root,
int iconResourceIndex, int localizedResourceIndex,
string iconResource = Shell32, string localizedResourceName = Shell32)
: this(root, null, iconResourceIndex, iconResource, localizedResourceIndex, localizedResourceName) { }
// Ctor without folder, without localizedRes
public DesktopIni(string root,
int iconResourceIndex, string iconResource = Shell32)
: this(root, null, iconResourceIndex, iconResource, 0, null) { }
// Ctor with folder, with localizedRes
public DesktopIni(string root, string folderName,
int iconResourceIndex, int localizedResourceIndex,
string iconResource = Shell32, string localizedResourceName = Shell32)
: this(root, folderName, iconResourceIndex, iconResource, localizedResourceIndex, localizedResourceName) { }
// Ctor with folder, without localizedRes
public DesktopIni(string root, string folderName,
int iconResourceIndex, string iconResource = Shell32)
: this(root, folderName, iconResourceIndex, iconResource, 0, null) { }
// Full Ctor
private DesktopIni(string root, string folderName,
int iconResourceIndex, string iconResource,
int localizedResourceIndex, string localizedResourceName)
{
if(!string.IsNullOrEmpty(folderName))
this.FullPath = Path.Combine(root, folderName);
else
this.FullPath = root;
this.IconResource = iconResource;
this.IconResourceIndex = iconResourceIndex;
this.LocalizedResourceName = localizedResourceName;
this.LocalizedResourceIndex = localizedResourceIndex;
}
public string FullPath;
public string IconResource;
public int IconResourceIndex;
public string LocalizedResourceName;
public int LocalizedResourceIndex;
}
"@
Add-Type -TypeDefinition $source
$Drive = $env:HOMEDRIVE + '\'
$Folders = @()
$Folders += New-Object DesktopIni "C:\Demo1", 160
$Folders += New-Object DesktopIni $Drive, "Demo2", 160
$Folders += New-Object DesktopIni "C:\Demo3", 160, -21813
$Folders += New-Object DesktopIni $Drive, "Demo4", 160, -21813
$Folders | Format-Table
异常:
New-Object : Index was outside the bounds of the array.
Au caractère C:\Users\Jeremy\Desktop\2 - Programs\test.ps1:69 : 13
+ $Folders += New-Object DesktopIni $Drive, "Demo2", 160
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-Object], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Com
mands.NewObjectCommand
请告诉我正确的语法,谢谢。
对于另外两行也适用相同的异常处理方法。