Skip to main content
 首页 » 编程设计

c#之C# 应用程序中的 System.ArgumentOutOfRangeException

2024年02月20日13zhenyulu

我已经实现了 Cuong 在这里建议的解决方案: C# Processing Fixed Width Files

我还让它通过一个文件夹并将其应用于该文件夹中的所有 .txt 文件。

一切正常,但对于某些 .txt 文件,它在 var csvLines 上失败并出现以下错误:

{"Index and length must refer to a location within the string.\r\nParameter name: length"} 
 
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll 
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. 
Parameter name: length 
   at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) 
   at System.String.Substring(Int32 startIndex, Int32 length) 
   at FixedWidthFiles.Main.<>c__DisplayClass11.<>c__DisplayClass13.<buttonProcessAllFiles_Click>b__d(KeyValuePair`2 pair) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 138 
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() 
   at System.String.Join(String separator, IEnumerable`1 values) 
   at FixedWidthFiles.Main.<>c__DisplayClass11.<buttonProcessAllFiles_Click>b__c(String line) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 137 
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() 
   at System.IO.File.InternalWriteAllLines(TextWriter writer, IEnumerable`1 contents) 
   at System.IO.File.WriteAllLines(String path, IEnumerable`1 contents) 
   at FixedWidthFiles.Main.buttonProcessAllFiles_Click(Object sender, EventArgs e) in \\GBMACCMPFS11\Shhk$\Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 140 

知道哪里出了问题吗?它可能是文件,但我希望可以在代码中更正/改进某些内容:)


代码是这样的:

private void buttonProcessAllFiles_Click(object sender, EventArgs e) 
{ 
    if (fileFolderPath == "") 
    { 
        MessageBox.Show("Load Folder First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
 
    } 
    else 
    { 
        int count = 0; 
        //foreach (var file in Directory.GetFiles(fileFolderPath, "*.txt", SearchOption.AllDirectories)) 
        foreach (var file in Directory.GetFiles(fileFolderPath, "*.txt")) 
        { 
            count++; 
            System.Diagnostics.Debug.WriteLine(count); 
            fileFolderFull = Path.GetFullPath(file); 
            System.Diagnostics.Debug.WriteLine(fileFolderFull); 
            fileFolderName = Path.GetFileNameWithoutExtension(file); 
            System.Diagnostics.Debug.WriteLine(fileFolderName); 
 
            //MessageBox.Show("Full Folder: " + fileFolderFull); 
            //MessageBox.Show("File Name: " + fileFolderName); 
 
            var lines = File.ReadAllLines(fileFolderFull); 
 
            var widthList = lines.First().GroupBy(c => c) 
                                         .Select(g => g.Count()) 
                                         .ToList(); 
 
            var list = new List<KeyValuePair<int, int>>(); 
 
            int startIndex = 0; 
 
            for (int i = 0; i < widthList.Count(); i++) 
            { 
                var pair = new KeyValuePair<int, int>(startIndex, widthList[i]); 
                list.Add(pair); 
 
                startIndex += widthList[i]; 
            } 
 
            try 
            { 
                var csvLines = lines.Select(line => string.Join(",", 
                                    list.Select(pair => line.Substring(pair.Key, pair.Value)))); 
 
                File.WriteAllLines(fileFolderPath + "\\" + fileFolderName + ".csv", csvLines); 
            } 
            catch (Exception ex) 
            { 
                System.Diagnostics.Debug.WriteLine(ex); 
            }  
        } 
 
        MessageBox.Show("File Saved", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 
} 

错误所在的行:

var csvLines = lines.Select(line => string.Join(",", 
                                        list.Select(pair => line.Substring(pair.Key, pair.Value)))); 

请您参考如下方法:

通读堆栈跟踪。第一个有趣的位置是:

Visual Studio 2010\Projects\FixedWidthFiles\FixedWidthFiles\Main.cs:line 138 

这是您的文件,包含您的代码。异常表示,您正在从大于字符串大小的索引中读取字符串。这都表明您的代码中存在错误。转到该文件并分析该行。一旦您考虑到这一点并且不知道为什么索引可能会在该位置超出范围,请复制该代码以及周围的一些代码,并将其发布在此处。仅凭堆栈跟踪很难说出更多信息。

编辑:你已经添加了代码,太棒了!

因为它在子字符串中肯定是失败的,所以看子字符串作为索引的是什么:它是来自对的值。该值来自列宽的增量求和,因此很可能您的输入文件之一只是有一行......太短了。检查文件的开头或结尾是否有虚假的空行!

您必须删除这些行的文件,或者针对它修复您的代码:不要盲目地调用子字符串,而是用 if 或 Math.min 保护它:

str.Substring( 
    Math.Min(str.Length, pair.Key), // here it MAY be needed a str.Length-1 instead! 
    Math.Min(Math.Max(0,str.Length-pair.Key), pair.Value) 
) 

这样,对于所有太短的行,字段切割器将返回空字符串。请注意,最好保护这两个参数,并且值得检查 Length-StartIndex 是否为负值,因为它们 StartIndex 可能大于空行的长度 :)

顺便说一句。通过 StartIndex 我当然是指 pair.Key..