C#应用程序中的System.ArgumentOutOfRangeException [英] System.ArgumentOutOfRangeException in C# Application

查看:999
本文介绍了C#应用程序中的System.ArgumentOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了Cuong在这里建议的解决方案: C#处理定宽文件

I have implemented the solution which Cuong suggested here: C# Processing Fixed Width Files

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

I have also made it go through a folder and apply that to all the .txt files in that folder.

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

All that works fine, but for some of the .txt files it fails on the var csvLines with the following error:

{"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

有什么想法吗?可能是文件,但我希望可以在代码中对某些内容进行纠正/改进:)

Any idea what is wrong? It might be the file, but I am hoping that something can be corrected/improved in the code :)

代码是这样的:

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

推荐答案

我认为Substring方法失败.您可以在该行上添加一个检查行吗?长度>(pair.Key + pair.Value)?

I think it's failing on the Substring method. Can you add a check that line.Length > (pair.Key + pair.Value)?

这篇关于C#应用程序中的System.ArgumentOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆