Someone help me with vb.net creating text files

Discussion in 'Programming' started by daves2369, Feb 21, 2009.

  1. #1
    hi i am making a program (console aplication) in vb.net for computing which is holaday homework it converts tempritures Celsius into Fahrenheit and i have basicly made the conversion and made it to loop until user presses x but i need to get the results into a text file which i am finding difficult to do and every time i try i fail :confused: this is my code:


    Option Explicit On
    Imports System
    Imports System.IO


    Module Module1

    Dim Celsius, Fahrenheit, sum As Single



    Sub Main()




    Do
    Dim strInput As String


    Console.Write("Celsius? ")
    strInput = Console.ReadLine()

    If UCase(strInput) = "X" Then
    Exit Do
    Else
    Celsius = CSng(strInput)
    sum = Celsius * 9 / 5 + 32

    Console.WriteLine("Fahrenheit is " & sum)
    Console.WriteLine("")
    Console.WriteLine("to do another conversion press enter ")
    Console.WriteLine("to exit press enter then type x")
    Console.WriteLine("")

    Console.ReadKey()


    End If
    Loop





    End Sub

    End Module





    could someone give me an worked example
    thanks for any help :)
     
    daves2369, Feb 21, 2009 IP
  2. bl4ckwolf

    bl4ckwolf Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Maybe try this :

    Public Function SaveTextToFile(ByVal strData As String, _
    ByVal FullPath As String, _
    Optional ByVal ErrInfo As String = "") As Boolean

    Dim Contents As String
    Dim bAns As Boolean = False
    Dim objReader As StreamWriter
    Try


    objReader = New StreamWriter(FullPath)
    objReader.Write(strData)
    objReader.Close()
    bAns = True
    Catch Ex As Exception
    ErrInfo = Ex.Message

    End Try
    Return bAns
    End Function
     
    bl4ckwolf, Feb 21, 2009 IP
  3. daves2369

    daves2369 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    iv tried and it dosnt like it


    when put the Dim Contents As String it comes up with an error

    and also where are you ment to put the directory of the file
     
    daves2369, Feb 22, 2009 IP
  4. bl4ckwolf

    bl4ckwolf Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    You will have to be more wise that simply doing cut and paste on this. If you got compilation error, fix them ;)

    The important code is this :

    Dim objReader As StreamWriter
    objReader = New StreamWriter(FullPath)
    objReader.Write(strData)
    objReader.Close()

    StrData is the string to write
    FullPath is the path to the file

    Have a nice day
     
    bl4ckwolf, Feb 22, 2009 IP