【发布时间】:2014-06-15 21:50:06
【问题描述】:
我这几天有问题,执行下面的脚本会导致执行。
我使用多个 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
其他 2 行也有同样的例外。
你能告诉我正确的语法吗,谢谢。
【问题讨论】:
-
这不是数组分配的问题。这是New-Object中使用的参数的问题。在数组赋值之外执行 New-Object 代码,你会看到同样的问题。您需要查看构造函数的编写方式或您传递的值。
标签: c# powershell