Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Closing File Stream

Closing File Stream

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mitreviper
    wrote on last edited by
    #1

    I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused: string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();

    P P 2 Replies Last reply
    0
    • M mitreviper

      I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused: string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();

      P Offline
      P Offline
      PaleyX
      wrote on last edited by
      #2

      Closing a reader or writer also closes the associated FileStream

      1 Reply Last reply
      0
      • M mitreviper

        I have the following and have the error when I try to close the FileStream. It says cannot access fs as it no longer exist. something like that. But I did not delete it.. I have this problem when I have the close the Stream Reader statement. Also, shouldn't the if statement work in checking if the FileStream still exist? I'm Confused :confused: string path = @"c:\TrayCount.txt"; fs = new FileStream( path, FileMode.OpenOrCreate , FileAccess.ReadWrite, FileShare.ReadWrite); sr = new StreamReader(fs); sw = new StreamWriter(fs); if(sr!=null) sr.Close(); if(sw!=null) sw.Close(); if(fs!=null) fs.Close();

        P Offline
        P Offline
        Pushkar Pathak
        wrote on last edited by
        #3

        well I checked the documentation for this and indeed it is right ... closing the streamreader/writer will close the stream associated with it. For some wierd reason till today I always use to assosciate one filestream to one reader / writer ... gladly never faced this problem I dont know if they got the implementation right. you are exclusively stating that you want FileShare mode and when u do that u expect that this filestream is going to be shared. I think they should have provided a reference count on SHARED system resource or atleast in IOStream .... espescially when it is explicitly indicate that u expect the filestream to be shared. might be worth taking up this to msdn Pushkar Pathak

        M 1 Reply Last reply
        0
        • P Pushkar Pathak

          well I checked the documentation for this and indeed it is right ... closing the streamreader/writer will close the stream associated with it. For some wierd reason till today I always use to assosciate one filestream to one reader / writer ... gladly never faced this problem I dont know if they got the implementation right. you are exclusively stating that you want FileShare mode and when u do that u expect that this filestream is going to be shared. I think they should have provided a reference count on SHARED system resource or atleast in IOStream .... espescially when it is explicitly indicate that u expect the filestream to be shared. might be worth taking up this to msdn Pushkar Pathak

          M Offline
          M Offline
          mitreviper
          wrote on last edited by
          #4

          The other thing i'm still confused is that of the if statement. Shouldn't if( fs!=null) be enough to check that the filestream has already closed?

          M 1 Reply Last reply
          0
          • M mitreviper

            The other thing i'm still confused is that of the if statement. Shouldn't if( fs!=null) be enough to check that the filestream has already closed?

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Why should closing a FileStream object set your reference to null? Calling Close on a FileStream means calling a method of an object, like every other method call. For your reference to be null after the call I'd assume there'd have to be the line this = null; somewhere in FileStream.Close() and I strongly doubt that this works... mav

            P 1 Reply Last reply
            0
            • M mav northwind

              Why should closing a FileStream object set your reference to null? Calling Close on a FileStream means calling a method of an object, like every other method call. For your reference to be null after the call I'd assume there'd have to be the line this = null; somewhere in FileStream.Close() and I strongly doubt that this works... mav

              P Offline
              P Offline
              Pushkar Pathak
              wrote on last edited by
              #6

              The Close () method in turns calls the Dispose() method and thats the reason why the filestream object is garbage collected... here is the msdn reference for it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassclosetopic.asp[^] Pushkar Pathak

              S 1 Reply Last reply
              0
              • P Pushkar Pathak

                The Close () method in turns calls the Dispose() method and thats the reason why the filestream object is garbage collected... here is the msdn reference for it http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassclosetopic.asp[^] Pushkar Pathak

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                Calling Dispose() doesn't make the object eligible for garbage collection. An object will be GC'ed only if there are no live references to it. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups