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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Determining file state (open or closed)

Determining file state (open or closed)

Scheduled Pinned Locked Moved C#
questioncsharptutorial
4 Posts 3 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.
  • N Offline
    N Offline
    Nitin1981
    wrote on last edited by
    #1

    Hi all! I'm using following code string[] arrWords; StreamReader objSr; StreamWriter objSw; try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtFileName.Text = openFileDialog1.FileName; objSr = new StreamReader(txtFileName.Text); string strFile = objSr.ReadToEnd(); arrWords = strFile.Split('\n'); objSr.Close(); objSw = new StreamWriter(txtFileName.Text, false); for (int i = 0; i < arrWords.Length; i++) { arrWords[i] = arrWords[i].ToLower(); objSw.WriteLine(arrWords[i]); } objSw.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (objSr != null) { objSr.Dispose(); } if (objSw != null) { objSw.Dispose(); } } 1. Now I want to check in finally block if file is opened (due to any exception it did not get closed) and close it(like we do with a Connection object in ADO.net) over there. How can I do this? 2. Also a warning comes if I use code like this that objSr and objSw is used before it has been assigned a value how to overcome it.? Thanks

    S S 2 Replies Last reply
    0
    • N Nitin1981

      Hi all! I'm using following code string[] arrWords; StreamReader objSr; StreamWriter objSw; try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtFileName.Text = openFileDialog1.FileName; objSr = new StreamReader(txtFileName.Text); string strFile = objSr.ReadToEnd(); arrWords = strFile.Split('\n'); objSr.Close(); objSw = new StreamWriter(txtFileName.Text, false); for (int i = 0; i < arrWords.Length; i++) { arrWords[i] = arrWords[i].ToLower(); objSw.WriteLine(arrWords[i]); } objSw.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (objSr != null) { objSr.Dispose(); } if (objSw != null) { objSw.Dispose(); } } 1. Now I want to check in finally block if file is opened (due to any exception it did not get closed) and close it(like we do with a Connection object in ADO.net) over there. How can I do this? 2. Also a warning comes if I use code like this that objSr and objSw is used before it has been assigned a value how to overcome it.? Thanks

      S Offline
      S Offline
      Steve
      wrote on last edited by
      #2

      I would move the call to objSw.Close() into the finally block, as it will always be executed there and then you don't have to worry about any exceptions that may or may not have occured. You should do this on most occasions to make sure that the connection gets closed correctly no matter what happens. To overcome the warning, set the objects to null on declaration. This means they are explicity set to null, and you're not inferring anything.

      N 1 Reply Last reply
      0
      • S Steve

        I would move the call to objSw.Close() into the finally block, as it will always be executed there and then you don't have to worry about any exceptions that may or may not have occured. You should do this on most occasions to make sure that the connection gets closed correctly no matter what happens. To overcome the warning, set the objects to null on declaration. This means they are explicity set to null, and you're not inferring anything.

        N Offline
        N Offline
        Nitin1981
        wrote on last edited by
        #3

        Thanks Steve! the second warning haunted me for a long time. Now I got at it. Regards Nitin

        1 Reply Last reply
        0
        • N Nitin1981

          Hi all! I'm using following code string[] arrWords; StreamReader objSr; StreamWriter objSw; try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtFileName.Text = openFileDialog1.FileName; objSr = new StreamReader(txtFileName.Text); string strFile = objSr.ReadToEnd(); arrWords = strFile.Split('\n'); objSr.Close(); objSw = new StreamWriter(txtFileName.Text, false); for (int i = 0; i < arrWords.Length; i++) { arrWords[i] = arrWords[i].ToLower(); objSw.WriteLine(arrWords[i]); } objSw.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (objSr != null) { objSr.Dispose(); } if (objSw != null) { objSw.Dispose(); } } 1. Now I want to check in finally block if file is opened (due to any exception it did not get closed) and close it(like we do with a Connection object in ADO.net) over there. How can I do this? 2. Also a warning comes if I use code like this that objSr and objSw is used before it has been assigned a value how to overcome it.? Thanks

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

          You could do away with the try/finally blocks and use the using statement instead.

          using (StreamReader objSr = new StreamReader(...))
          {
          using (StreamWriter objSW = new StreamWriter(...))
          {
          ...
          }
          }

          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