在C#4.0版System.ObjectDisposedException错误 [英] System.ObjectDisposedException Error on C# v4.0

查看:1073
本文介绍了在C#4.0版System.ObjectDisposedException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了这个代码:
C#处理固定宽度文件

不过,由于我的工作电脑运行的是Windows XP,我不得不节约使用C#4.0版程序。不幸的是,下面的代码:

But since my work PC runs Windows XP I had to save the program using C# v4.0. Unfortunately the following code:

static string filePath = "";

public Main()
{
    InitializeComponent();
}

private void buttonLoadFile_Click(object sender, EventArgs e)
{
    DialogResult openFile = openFileDialog.ShowDialog();
    if (openFile == DialogResult.OK)
    {
        filePath = openFileDialog.FileName;
    }
}

private void buttonProcessFile_Click(object sender, EventArgs e)
{
    if (filePath == "")
    {
        MessageBox.Show("Load Fixed Width File First", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }
    else
    {
        if (textboxFilePath.Text == "")
        {
            MessageBox.Show("Enter CSV File Path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            DialogResult result = DialogResult.No;

            if (File.Exists(filePath))
            {
                result = MessageBox.Show("Overwrite CSV File?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }

            if (result == DialogResult.Yes)
            {
                var lines = File.ReadLines(filePath);

                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];
                }

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

                File.WriteAllLines(textboxFilePath.Text, csvLines);

                MessageBox.Show("File Saved", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}

在Windows XP上运行,并在C#编译V4.0给我这个错误:

Gives me this error when run on Windows XP and compiled in C# v4.0:

************** Exception Text **************
System.ObjectDisposedException: Cannot read from a closed TextReader.
   at System.IO.__Error.ReaderClosed()
   at System.IO.StreamReader.ReadLine()
   at System.IO.File.<InternalReadLines>d__0.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`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.buttonProcessFile_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
FixedWidthFiles
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/TEMP/FixedWidthFiles.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------

有什么建议?

推荐答案

MSDN 文件

方法不同作为readlines方法和ReadAllLines如下:当您使用
readlines方法,你就可以开始
的前枚举字符串的集合整个集合返回;当您使用ReadAllLines,您必须
等字符串全阵列退回,然后才能访问$ B $数组b。因此,当您正在使用非常大的文件时,
readlines方法可以更有效。

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

更改代码,

var widthList = File.ReadLines(@"C:\input.txt").First().GroupBy(c => c)
                             .Select(g => g.Count())
                             .ToList();



或者使用

Or use

var lines = File.ReadAllLines(@"C:\input.txt");

var widthList = lines.First().GroupBy(c => c)
                             .Select(g => g.Count())
                             .ToList();

这篇关于在C#4.0版System.ObjectDisposedException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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