Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.ObjectDisposedException Error on C# v4.0
    primarykey
    data
    text
    <p>I implemented this code: <a href="https://stackoverflow.com/questions/12543223/c-sharp-processing-fixed-width-files">C# Processing Fixed Width Files</a></p> <p>But since my work PC runs Windows XP I had to save the program using C# v4.0. Unfortunately the following code:</p> <pre><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 =&gt; c) .Select(g =&gt; g.Count()) .ToList(); var list = new List&lt;KeyValuePair&lt;int, int&gt;&gt;(); int startIndex = 0; for (int i = 0; i &lt; widthList.Count(); i++) { var pair = new KeyValuePair&lt;int, int&gt;(startIndex, widthList[i]); list.Add(pair); startIndex += widthList[i]; } var csvLines = lines.Select(line =&gt; string.Join(",", list.Select(pair =&gt; line.Substring(pair.Key, pair.Value)))); File.WriteAllLines(textboxFilePath.Text, csvLines); MessageBox.Show("File Saved", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } </code></pre> <p>Gives me this error when run on Windows XP and compiled in C# v4.0:</p> <pre><code>************** Exception Text ************** System.ObjectDisposedException: Cannot read from a closed TextReader. at System.IO.__Error.ReaderClosed() at System.IO.StreamReader.ReadLine() at System.IO.File.&lt;InternalReadLines&gt;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&amp; m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m) at System.Windows.Forms.Button.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; 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 ---------------------------------------- </code></pre> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload