Writing text to file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Writing text to file

    Hi

    I am trying to set-up an app log file that needs to be created if does not
    exists but opened for append if exists. How does one go about doing this?
    ..net seems to have many ways to do io stream, boggles the mind.

    Thanks

    Regards


  • Mike Lowery

    #2
    Re: Writing text to file


    "John" <John@nospam.in fovis.co.ukwrot e in message
    news:%23nmQiF4y GHA.3584@TK2MSF TNGP02.phx.gbl. ..
    Hi
    >
    I am trying to set-up an app log file that needs to be created if does not
    exists but opened for append if exists. How does one go about doing this? .net
    seems to have many ways to do io stream, boggles the mind.
    If File.Exists("c: \myfile.txt") Then
    'append
    Else
    'create
    End If


    Comment

    • John

      #3
      Re: Writing text to file

      Hi

      I have done this much already;

      Dim JobsFS As FileStream
      If File.Exists(Job sFSPath) Then
      JobsFS = File.Open(JobsF SPath, FileMode.Append )
      Else
      JobsFS = File.Create(Job sFSPath)
      End If

      What I don't understand is how to write some text into it, like
      JobsFS.Write("T his is some text") or something similar.

      Thanks

      Regards


      "Mike Lowery" <selfspam@mou se-potato.comwrote in message
      news:%23vxHhL4y GHA.4408@TK2MSF TNGP05.phx.gbl. ..
      >
      "John" <John@nospam.in fovis.co.ukwrot e in message
      news:%23nmQiF4y GHA.3584@TK2MSF TNGP02.phx.gbl. ..
      >Hi
      >>
      >I am trying to set-up an app log file that needs to be created if does
      >not exists but opened for append if exists. How does one go about doing
      >this? .net seems to have many ways to do io stream, boggles the mind.
      >
      If File.Exists("c: \myfile.txt") Then
      'append
      Else
      'create
      End If
      >
      >

      Comment

      • Andrew Morton

        #4
        Re: Writing text to file

        John wrote:
        I have done this much already;
        >
        Dim JobsFS As FileStream
        If File.Exists(Job sFSPath) Then
        JobsFS = File.Open(JobsF SPath, FileMode.Append )
        Else
        JobsFS = File.Create(Job sFSPath)
        End If
        >
        What I don't understand is how to write some text into it, like
        JobsFS.Write("T his is some text") or something similar.
        Imports System.IO
        Dim fsw As New FileStream(curr entLog, FileMode.OpenOr Create,
        FileAccess.Writ e, FileShare.Read)
        fsw.Position = fsw.Length
        Dim w As StreamWriter = New StreamWriter(fs w)
        w.WriteLine(Dat eTime.Now.ToLon gTimeString() & vbTab & s)
        w.Close()

        where currentLog is the path-and-filename of the file to create/append to
        and the variable s is the message to write.

        HTH

        Andrew


        Comment

        • Miro

          #5
          Re: Writing text to file

          Is something like this you are looking for ?
          -A simple function that you pass in some text. It makes sure the file is
          there, if not it creates it and then "APPENDS" the
          text to the end of the file.
          =============== ==
          Public Function WriteTextFile(B yVal ExtraString As String)
          Dim FileName As String = "Text.txt"
          Dim oTextFile As System.IO.File
          Dim oTextWriteText As System.IO.Strea mWriter
          'Check if file exists...if not create it.
          If Not System.IO.File. Exists(FileName ) Then
          oTextWriteText = oTextFile.Creat eText(FileName)
          oTextWriteText. Close()
          End If

          'If something passed in
          If Not ExtraString = String.Empty Then
          'Blank Line between last line.
          oTextWriteText. WriteLine()
          oTextWriteText. WriteLine( ExtraString )
          oTextWriteText. WriteLine()
          End If
          'Closes the Text File.
          oTextWriteText. Close()
          End Function
          =============== ==


          Miro







          "John" <John@nospam.in fovis.co.ukwrot e in message
          news:O27b9Y4yGH A.4232@TK2MSFTN GP05.phx.gbl...
          Hi
          >
          I have done this much already;
          >
          Dim JobsFS As FileStream
          If File.Exists(Job sFSPath) Then
          JobsFS = File.Open(JobsF SPath, FileMode.Append )
          Else
          JobsFS = File.Create(Job sFSPath)
          End If
          >
          What I don't understand is how to write some text into it, like
          JobsFS.Write("T his is some text") or something similar.
          >
          Thanks
          >
          Regards
          >
          >
          "Mike Lowery" <selfspam@mou se-potato.comwrote in message
          news:%23vxHhL4y GHA.4408@TK2MSF TNGP05.phx.gbl. ..
          >>
          >"John" <John@nospam.in fovis.co.ukwrot e in message
          >news:%23nmQiF4 yGHA.3584@TK2MS FTNGP02.phx.gbl ...
          >>Hi
          >>>
          >>I am trying to set-up an app log file that needs to be created if does
          >>not exists but opened for append if exists. How does one go about doing
          >>this? .net seems to have many ways to do io stream, boggles the mind.
          >>
          >If File.Exists("c: \myfile.txt") Then
          >'append
          >Else
          >'create
          >End If
          >>
          >>
          >
          >

          Comment

          • HKSHK

            #6
            Re: Writing text to file

            Hi John,

            If you use the StreamWriter class, you don't have to worry if the file
            exists or not.

            Dim path As String = "c:\temp\MyTest .txt"

            Dim sw As New StreamWriter(pa th, True)
            sw.WriteLine("T his")
            sw.WriteLine("i s a test.")
            sw.Flush()
            sw.Close()

            Best Regards,

            HKSHK

            John wrote:
            Hi
            >
            I am trying to set-up an app log file that needs to be created if does not
            exists but opened for append if exists. How does one go about doing this?
            .net seems to have many ways to do io stream, boggles the mind.
            >
            Thanks
            >
            Regards
            >
            >

            Comment

            • Oenone

              #7
              Re: Writing text to file

              John wrote:
              I am trying to set-up an app log file that needs to be created if
              does not exists but opened for append if exists. How does one go
              about doing this? .net seems to have many ways to do io stream,
              boggles the mind.
              If you're using VS2005, you can solve this using the much simpler
              System.IO.File. AppendAllText() method. For example:

              \\\
              IO.File.AppendA llText(Filename , Content)
              ///

              This will create the file specified in Filename if it doesn't already exist,
              or append to it if it does. The contents of the string variable Content will
              then be added to the end of the file.

              HTH,

              --

              (O)enone


              Comment

              • Greg

                #8
                Re: Writing text to file

                "John" <John@nospam.in fovis.co.ukwrot e in message
                news:%23nmQiF4y GHA.3584@TK2MSF TNGP02.phx.gbl. ..
                Hi
                >
                I am trying to set-up an app log file that needs to be created if does not
                exists but opened for append if exists. How does one go about doing this?
                .net seems to have many ways to do io stream, boggles the mind.
                >
                Thanks
                >
                Regards
                Hi John,
                I use something like the following sub to write to my log file.
                If the file doesn't exist then the Append command will create it anyway.
                In the code, I'd call sbLog ("Commenced reading input file") etc.

                Private Sub sbLog(ByVal sMsg As String)

                'Write entry to the log file
                Dim sFilename As String
                Dim sData As String
                Dim iFileNo As Integer

                sFilename = "C:\LogFile s\" & Application.Pro ductName & ".log"
                sData = Format(Now, "Short Time") & " " & sMsg
                iFileNo = FreeFile()
                FileOpen(iFileN o, sFilename, OpenMode.Append )
                Print(iFileNo, sData & vbCrLf)
                FileClose(iFile No)

                End Sub

                Cheers,
                Greg




                Comment

                • ShaneO

                  #9
                  Re: Writing text to file

                  Oenone wrote:
                  John wrote:
                  >I am trying to set-up an app log file that needs to be created if
                  >does not exists but opened for append if exists. How does one go
                  >about doing this? .net seems to have many ways to do io stream,
                  >boggles the mind.
                  >
                  If you're using VS2005, you can solve this using the much simpler
                  System.IO.File. AppendAllText() method. For example:
                  >
                  \\\
                  IO.File.AppendA llText(Filename , Content)
                  ///
                  >
                  This will create the file specified in Filename if it doesn't already exist,
                  or append to it if it does. The contents of the string variable Content will
                  then be added to the end of the file.
                  >
                  HTH,
                  >
                  In a similar way as Oenone, I use the following -

                  Sub LogFile(ByVal sMsg As String)
                  sMsg &= vbCrLf
                  My.Computer.Fil eSystem.WriteAl lText(sFileName , sMsg, True)
                  End Sub

                  This also automatically handles creating or appending of the Log File
                  and is a simple solution.

                  ShaneO

                  There are 10 kinds of people - Those who understand Binary and those who
                  don't.

                  Comment

                  Working...