1

我已经实现了 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))));
4

2 回答 2

3

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

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)
)

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

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

于 2012-10-02T22:27:42.830 回答
1

我认为它在 Substring 方法上失败了。您可以添加检查 line.Length > (pair.Key + pair.Value) 吗?

于 2012-10-02T22:30:57.793 回答