locked
write to text file RRS feed

  • Question

  • User-321013422 posted

     Why can't my code write to a text file???It needs to write the name of a  

    1    Imports System.IO
    2    Imports System
    3    Module Module1
    4    
    5        Sub Main()
    6            Dim writeText As TextWriter
    7            Dim caller As String = ""
    8            Dim sWriter As StreamWriter = New StreamWriter("C:\Projects\call_log.txt")
    9            Dim name As String = ""
    10   
    11   
    12   
    13           Console.WriteLine(name)
    14           name = Console.ReadLine()
    15           Console.WriteLine(Now.ToString, name & "Called Right Now" & Now.Date)
    16           name = sWriter
    17   
    18           Console.ReadLine()
    19   
    20   
    21       End Sub
    22   
    23   End Module
    
     person to the text file??
    Friday, September 26, 2008 10:43 AM

Answers

  • User1485238302 posted

    Thats because you are not writing to the file. You have to use Write method to write to it.

    http://www.java2s.com/Code/CSharp/File-Stream/OpenafileusingStreamWriter.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:56 AM
  • User-1757793930 posted

    With a stream writer you have to call its write method, you are calling the console write which will write to the output stream (your console window) you will need to write to the file itself try this

    sWriter.Write("This is some sample text written in a line")
    sWriter.Write(sWriter.NewLine) 'fiinish writing that line
    sWriter.WriteLine("This is a full text line.")
    sWriter.Close 'Close the writer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:57 AM
  • User-470797114 posted

    Friend, try this

    1. Start Visual Studio 2005 or Visual Studio .NET.
    2. Create a new Console Application in Visual Basic 2005 or Visual Basic .NET.
    3. Add the following code to the top of Module1.vb:

    Imports System.IO
         

    4. Add the following code to the Sub Main procedure:

    Dim objStreamWriter As StreamWriter
       
       'Pass the file path and the file name to the StreamWriter constructor.
       objStreamWriter = New StreamWriter("C:\Testfile.txt")

       'Write a line of text.
       objStreamWriter.WriteLine("Hello World")

       'Write a second line of text.
       objStreamWriter.WriteLine("From the StreamWriter class")

       'Close the file.
       objStreamWriter.Close()
         

    5. On the Debug menu, click Start to compile and to run the application.

    This code creates a file named Test.txt on drive C. Open Test.txt in a text editor such as Notepad. Test.txt contains two lines of text:

    Hello World
    From the StreamWriter class

    More info : http://support.microsoft.com/kb/315828

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:58 AM
  • User533502567 posted

     Try this code..

        Imports System.IO
        Imports System
        Module Module1
        
            Sub Main()
                Dim writeText As TextWriter
    
                Dim caller As String = ""
                Dim sWriter As StreamWriter = New StreamWriter("C:\Projects\call_log.txt")
                Dim name As String = ""
          
                Console.WriteLine(name)
               name = Console.ReadLine()
    
                //add these 3 lines to your program
               sw.WriteLine(name) 
               sw.Flush()
               sw.Close()
    
               Console.WriteLine(Now.ToString, name & "Called Right Now" & Now.Date)
               //name = sWriter
     
              Console.ReadLine()
          
           End Sub
       
       End Module
    
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 11:02 AM

All replies

  • User1485238302 posted

    Thats because you are not writing to the file. You have to use Write method to write to it.

    http://www.java2s.com/Code/CSharp/File-Stream/OpenafileusingStreamWriter.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:56 AM
  • User-1757793930 posted

    With a stream writer you have to call its write method, you are calling the console write which will write to the output stream (your console window) you will need to write to the file itself try this

    sWriter.Write("This is some sample text written in a line")
    sWriter.Write(sWriter.NewLine) 'fiinish writing that line
    sWriter.WriteLine("This is a full text line.")
    sWriter.Close 'Close the writer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:57 AM
  • User-470797114 posted

    Friend, try this

    1. Start Visual Studio 2005 or Visual Studio .NET.
    2. Create a new Console Application in Visual Basic 2005 or Visual Basic .NET.
    3. Add the following code to the top of Module1.vb:

    Imports System.IO
         

    4. Add the following code to the Sub Main procedure:

    Dim objStreamWriter As StreamWriter
       
       'Pass the file path and the file name to the StreamWriter constructor.
       objStreamWriter = New StreamWriter("C:\Testfile.txt")

       'Write a line of text.
       objStreamWriter.WriteLine("Hello World")

       'Write a second line of text.
       objStreamWriter.WriteLine("From the StreamWriter class")

       'Close the file.
       objStreamWriter.Close()
         

    5. On the Debug menu, click Start to compile and to run the application.

    This code creates a file named Test.txt on drive C. Open Test.txt in a text editor such as Notepad. Test.txt contains two lines of text:

    Hello World
    From the StreamWriter class

    More info : http://support.microsoft.com/kb/315828

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 10:58 AM
  • User533502567 posted

     Try this code..

        Imports System.IO
        Imports System
        Module Module1
        
            Sub Main()
                Dim writeText As TextWriter
    
                Dim caller As String = ""
                Dim sWriter As StreamWriter = New StreamWriter("C:\Projects\call_log.txt")
                Dim name As String = ""
          
                Console.WriteLine(name)
               name = Console.ReadLine()
    
                //add these 3 lines to your program
               sw.WriteLine(name) 
               sw.Flush()
               sw.Close()
    
               Console.WriteLine(Now.ToString, name & "Called Right Now" & Now.Date)
               //name = sWriter
     
              Console.ReadLine()
          
           End Sub
       
       End Module
    
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 26, 2008 11:02 AM