VB Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    VB Question

    I think I need a good programming forum. I'm frustrated. Anyhoo, given this code:

    Code:
    Dim outLogPath As String = "C:\logfile.txt"
     Dim outLogStream As System.IO.StreamWriter
    
     If Not IO.File.Exists(outLogPath) Then
                        outDataStream = System.IO.File.CreateText(outLogPath)
                    Else : outLogStream = New System.IO.StreamWriter(outLogPath, False)
                    End If
     outLogStream.WriteLine("Log File Created")
    The file gets created but doesn't get written to.
    What am I missing?
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • tjmac44
    Forum Newbie
    • Nov 2006
    • 76
    • Omaha, Nebraska

    #2
    Originally posted by crokett
    I think I need a good programming forum. I'm frustrated. Anyhoo, given this code:

    Code:
    Dim outLogPath As String = "C:\logfile.txt"
     Dim outLogStream As System.IO.StreamWriter
    
     If Not IO.File.Exists(outLogPath) Then
                        outDataStream = System.IO.File.CreateText(outLogPath)
                    Else : outLogStream = New System.IO.StreamWriter(outLogPath, False)
                    End If
     outLogStream.WriteLine("Log File Created")
    The file gets created but doesn't get written to.
    What am I missing?
    I am a Java guy, but if I read this correctly the very first time you run this, or the file C:\logfile.txt does not exists, the program will create a file called logfile.txt, but it will not create the object outLogStream. Does this throw a runtime exception when outLogStream is referenced after the "End if" without having been created with the New keyword?

    What happens if you run this 2 times in a row?

    I dont know the VB good enough to help you out much more.
    Todd

    Grounded in fly-over country.

    Comment

    • tjmac44
      Forum Newbie
      • Nov 2006
      • 76
      • Omaha, Nebraska

      #3
      Originally posted by crokett
      I think I need a good programming forum. I'm frustrated. Anyhoo, given this code:

      Code:
      Dim outLogPath As String = "C:\logfile.txt"
       Dim outLogStream As System.IO.StreamWriter
      
       If Not IO.File.Exists(outLogPath) Then
                          outDataStream = System.IO.File.CreateText(outLogPath)
                      Else : outLogStream = New System.IO.StreamWriter(outLogPath, False)
                      End If
       outLogStream.WriteLine("Log File Created")
      The file gets created but doesn't get written to.
      What am I missing?
      Not having a VB compiler, try something like this:

      Dim outLogPath As String = "C:\logfile.txt"
      Dim outLogStream As System.IO.StreamWriter

      If Not IO.File.Exists(outLogPath) Then
      outDataStream = System.IO.File.CreateText(outLogPath)
      End If
      outLogStream = New System.IO.StreamWriter(outLogPath, False)
      outLogStream.WriteLine("Log File Created")
      Todd

      Grounded in fly-over country.

      Comment

      • Hoakie
        Established Member
        • Feb 2007
        • 382
        • Iowa
        • Craftsman 21829

        #4
        I am assuming you are trying to create the log file if it doesn't exist and append to it if it does. What you didn't do was call the Close method on the StreamWriter class.

        Here is a cleaned up version of your code that works for me

        Dim outLogPath As String = "C:\logfile.txt"
        Dim outLogStream As System.IO.StreamWriter

        If Not IO.File.Exists(outLogPath) Then
        outLogStream = New System.IO.StreamWriter(outLogPath)
        Else
        outLogStream = New System.IO.StreamWriter(outLogPath, True)
        End If
        outLogStream.WriteLine("Log File Created")
        outLogStream.Close()
        John
        To invent, you need a good imagination and a pile of junk. ~ Edison

        Comment

        • crokett
          The Full Monte
          • Jan 2003
          • 10627
          • Mebane, NC, USA.
          • Ryobi BT3000

          #5
          Thanks John. I tried that using the close() function. It worked but then of course the code threw an exception the next time I tried to write to the file in the code. I need to write to the file while the program is running. It seems messy to close() and then repopen the stream every time I want to write a line. I tried using Flush() and that didn't work.
          Last edited by crokett; 11-15-2008, 12:05 PM.
          David

          The chief cause of failure in this life is giving up what you want most for what you want at the moment.

          Comment

          • Hoakie
            Established Member
            • Feb 2007
            • 382
            • Iowa
            • Craftsman 21829

            #6
            the alternative is to open the stream when the app opens, write to it throughout then close the stream when the app exits. However, the stream functions are pretty efficient so I would think for logging it would be better to open/close each time. This would insure you have captured all your log data and not lose it if the app crashes.

            my 2cents
            John
            To invent, you need a good imagination and a pile of junk. ~ Edison

            Comment

            • Alex Franke
              Veteran Member
              • Feb 2007
              • 2641
              • Chapel Hill, NC
              • Ryobi BT3100

              #7
              This is how I would do it:

              Code:
              System.IO.File.AppendAllText( @"C:\logfile.txt", "Log file created.\r\n" );
              This will create the file if it doesn't already exist, open the file, append the string to the end, and close the file -- everything you need it to do.

              If you really really want to do it the hard way, you need to flush the stream, then close it. Also a file stream is a precious resource, so you don't want to keep an open handle to it any longer than you need to. Wrap the code in a "using" statement so the object will get explicitly finalized.

              Just open/write/close -- there's nothing wrong with that. It's really the safest and cleanest way to do it.

              If you're writing out a lot to a log file and you find that it is bogging down resources (test it first to be sure it actually is), then you have a couple of options: (1) Keep the filestream open, or (2) collect all the data you want to write it all at the same time at the end. The drawback of option 1 is that you're hanging on to system resources -- and it might not really benefit you that much, and the drawback of option 2 is that if your app crashes, you could lose a lot of log entries.
              online at http://www.theFrankes.com
              while ( !( succeed = try() ) ) ;
              "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

              Comment

              • Alex Franke
                Veteran Member
                • Feb 2007
                • 2641
                • Chapel Hill, NC
                • Ryobi BT3100

                #8
                Whoops -- that was in C#. Here it is in VB. It's just different syntax.

                Code:
                System.IO.File.AppendAllText( "C:\logfile.txt", "Log file created." + Environment.NewLine )
                online at http://www.theFrankes.com
                while ( !( succeed = try() ) ) ;
                "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

                Comment

                • crokett
                  The Full Monte
                  • Jan 2003
                  • 10627
                  • Mebane, NC, USA.
                  • Ryobi BT3000

                  #9
                  Thanks Alex, that is a great suggestion and I will do that.
                  David

                  The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                  Comment

                  Working...