Announcement

Collapse
No announcement yet.

Example of writing to a file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example of writing to a file

    Team,

    Can anyone share some working sample .vb code that writes text to a new or existing file?

    I have done this on IIS before but it doesn't run on the HS webserver.

    Thank you!
    DJ
    Last edited by DJF3; January 4th, 2010, 04:20 AM.

  • #2
    Here's a .net sub to write to a log file

    Code:
    Public Sub PrintToFile(ByVal msg)
            Dim oSW As StreamWriter
            oSW = File.AppendText(Server.MapPath(ConfigurationManager.AppSettings("LogFile").ToString()))
    
            oSW.WriteLine(msg)
            oSW.Close()
       End Sub
    Just change Server.MapPath(ConfigurationManager.AppSettings("LogFile").T oString()) to the path of the output file.
    -Rupp
    sigpic

    Comment


    • #3
      These are one liners:

      To append to an existing file:

      PHP Code:
      My.Computer.FileSystem.WriteAllText(filepath,textstring,True
      To overwrite a file:

      PHP Code:
      My.Computer.FileSystem.WriteAllText(filepath,textstring,False
      Jon

      Comment


      • #4
        Thanks,
        With your help I have been able to create the following code that writes text to a file:

        Code:
        Imports System.IO
        Imports System.IO.StreamReader 
        
        Sub Main(ByVal varParameters)
        
            Const strDebug = 1
            Const strFilename = "C:\Program Files\HomeSeer 2\data\DJ\ftpthis.txt"
        
            If File.Exists(strFilename) then File.Delete(strFilename)
            PrintToFile(strFilename,"my string written to file")
        
        End Sub
        
        
        Function PrintToFile(strFilename,msg)
                Dim oSW As StreamWriter
                oSW = File.AppendText(strFilename.ToString())
                oSW.WriteLine(msg)
                oSW.Close()
        End Function
        (haven't tested the exact code above yet because it's part of a bigger script that now works - which I will soon share with you)

        Comment

        Working...
        X