Click here to Skip to main content
15,884,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
FileStream obj = new FileStream(System.Convert.ToString(row.Cells["Lec_media"].Value), FileMode.Open, FileAccess.Read, FileShare.Read);



 if (Startdate2 == JCM_NOW2)
                {
                    if (checkBox1.Checked == true)
                    {

                        try
                        {


                            obj = null;
                            System.Diagnostics.Process.Start(System.Convert.ToString(row.Cells["Lec_media"].Value));

                        }
                        finally
                        {
                            if (obj != null)
                            obj.Close();

                        }
                    }

                }



            if (Enddate2 == JCM_NOW2)
            {



                if (obj != null) ((IDisposable)obj).Dispose();

                obj.Close();

               // this code does not close the file which opened above .. why?


            }
Posted
Comments
George Swan 11-Feb-15 16:59pm    
I think it is because you have called obj.Dispose() before closing it.
Golden Basim 11-Feb-15 17:56pm    
the same error after removing if (obj != null) ((IDisposable)obj).Dispose();

1 solution

Use different pattern with stream, without explicit closing (I know, the documentation is a bit confusing):
C#
using (FileStream stream = new FileStream( /* ... */ )) {
   // use stream object here
} // stream.Dispose() is automatically called here


Besides, regardless of the closing problems, it's important to guarantee that you eventually call System.IDisposable.Dispose() for each and every object implementing this interface. That's why it's good to use the using statement (not to be confused with using directive!): https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

One more suggestion: most likely, you don't need to use FileStream directly. It is really needed only if you have read/write stream, which is a rare case; usually, your stream is read-only or write-only. Therefore, it's usually much better to use one or more of these classes:
https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.binarywriter%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Golden Basim 11-Feb-15 17:39pm    
how can i use (using) in more than scope ?

if ( .... )
{
using (FileStream stream = new FileStream( /* ... */ )) {
// use stream object here
}

if(.....)
{

// Dispose

}
}
Sergey Alexandrovich Kryukov 11-Feb-15 19:22pm    
And what's the problem? Just read about "using". This is the same kind of statement and any other block statement...
—SA
Golden Basim 11-Feb-15 18:00pm    
the same error :


using (FileStream fileStream_1 = new FileStream(JCM_File_Path_01, FileMode.Open, FileAccess.Read, FileShare.Read))
{

if (Startdate2 == JCM_NOW2)
{
if (checkBox1.Checked == true)
{
try
{
fileStream_1 = null;
System.Diagnostics.Process.Start(System.Convert.ToString(row.Cells["Lec_media"].Value));
}
finally
{
if (fileStream_1 != null)
fileStream_1.Close();
}
}

}



if (Enddate2 == JCM_NOW2)
{
// if (fileStream_1 != null) ((IDisposable)fileStream_1).Dispose();
fileStream_1.Close();

// this code does not close the file which opened above .. why?


}
}
Andreas Gieriet 11-Feb-15 18:23pm    
You seem not to understand several parts of the solution.
1) The using construct *does* call Dispose on the instance (this is the only purpose of using!).
2) Dispose closes the stream (see the online documentation).
Andi
Sergey Alexandrovich Kryukov 11-Feb-15 19:23pm    
Exactly.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900