2 Replies - 610 Views - Last Post: 26 March 2012 - 06:19 AM Rate Topic: -----

#1 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

.dispose not working

Posted 24 March 2012 - 11:26 PM

I am creating a simple program that reads in a stream. Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IDoNotKnow
{
    public partial class Form1 : Form
    {
        private string pathOfFile;
        public Form1()
        {
            InitializeComponent();
            WriteBtn.Enabled = false;
            textCont.Enabled = false;
        }

        private void OpnBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog forReading = new OpenFileDialog();
            forReading.Filter= "Text File|*.txt";
            if (forReading.ShowDialog() == DialogResult.OK)
            {
                WriteBtn.Enabled = true;
                pathOfFile = forReading.FileName;
                textCont.BackColor = Color.White;
                textCont.ForeColor = Color.Black;
            }
            StreamReader reader = new StreamReader(pathOfFile);
            textCont.Text = reader.ReadToEnd();
            forReading.Dispose(); 
        }

        private void WriteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                StreamWriter forWriting = new StreamWriter(pathOfFile);
                forWriting.Write(textCont.Text.ToString());
                forWriting.Dispose();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error!");
            }
        }

        private void EnableEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (EnableEdit.Checked) {textCont.Enabled = true;}
            else { textCont.Enabled = false; }
        }
    }
}





The problem is that when I invoke WriteBtn_Click, the exception occurs. It says that the file is still being used by another process. Shouldn't that be, as I have already disposed of the forReading stream? What is the problem?

Is This A Good Question/Topic? 0
  • +

Replies To: .dispose not working

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: .dispose not working

Posted 25 March 2012 - 03:14 AM

Go with using blocks.

try
{
    using (StreamWriter sw = new StreamWriter(pathOfFile))
    {
        // Do your thing
        // The stream is automatically flushed (in an output stream)
        // closed, and disposed upon leaving this block.
    }
}
catch (Exception ex)
{

}


Guaranteed proper cleanup.
Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: .dispose not working

Posted 26 March 2012 - 06:19 AM

The problem is that forReading isn't a stream! You're disposing the OpenFileDialog. Which is fine, but it has nothing to do with the stream object. You have to dispose that too.

Which, btw, I also suggest you use in a using block. You can use any IDisposable object inside a using block, and when it leaves the block, Dispose is called.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1