0

我收到错误“索引(从零开始)必须大于或等于零且小于参数列表的大小。” 我不知道为什么。

我的代码如下:

'read directory and look for filenames that match pattern and have code elements from xml file

    Dim regElemName As New Regex("^code")
    Dim root = XElement.Load(xmlfile)
    Dim codeElements = root.Element("ApplicationSettings").Elements().Where(Function(xe) regElemName.IsMatch(xe.Name.LocalName)).Select(Function(xe) xe.Value)
    Dim codes = String.Join("|", codeElements.ToArray())
    Dim regFileName As New Regex(String.Format("^\d{5}\-(?<Year>(?:\d{4}))(?<Month>0?[1-9]|12|11|10)(?<Day>[12]\d|0?[1-9]|3[01])\-$", codes))
    Dim files = IO.Directory.GetFiles(TextBox1.Text, "*.pdf", IO.SearchOption.TopDirectoryOnly).Where(Function(path) regFileName.IsMatch(IO.Path.GetFileName(path)))

    For Each file As String In files
        Console.WriteLine(file)
    Next

有任何想法吗?

4

1 回答 1

6

String.Format("^\d{5}\-(?<Year>(?:\d{4}))(?<Month>0?[1-9]|12|11|10)(?<Day>[12]\d|0?[1-9]|3[01])\-$", codes)

您在格式字符串中引用{5}and {4},但您只提供一个参数codes。4 和 5 将分别引用传递给的第 5 个和第 6 个参数string.format

您收到此错误是因为您的格式字符串要求您传递至少 6 个参数(基于您在格式字符串中引用的最大索引)。

于 2013-05-31T16:53:35.267 回答