Results 1 to 3 of 3

Thread: Writing to a file with a subroutine

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    2

    Writing to a file with a subroutine

    Here's the code, why doesn't it work?

    Code:
        Public Sub SaveSettings(ByVal Path As String, ByVal Interval As Integer, ByVal Startup As Boolean)
            If File.Exists(Application.StartupPath & "\Settings") Then
                File.Delete(Application.StartupPath & "\Settings")
            End If
    
            Dim oFile As FileStream
            Dim oWrite As StreamWriter
    
            oFile = New FileStream(Application.StartupPath & "\Settings", FileMode.Create, FileAccess.Write)
            oWrite = New StreamWriter(oFile)
    
            oWrite.Write(Path)
            'oWrite.WriteLine(Interval)
            'oWrite.WriteLine(Startup.ToString)
    
            oFile.Close()
            oFile = Nothing
            oWrite = Nothing
        End Sub

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Writing to a file with a subroutine

    My guess is that its not working because you dont have an extension on your file.

    VB Code:
    1. Public Sub SaveSettings(ByVal Path As String, ByVal Interval As Integer, ByVal Startup As Boolean)
    2.         If File.Exists(Application.StartupPath & "\Settings.txt") Then
    3.             File.Delete(Application.StartupPath & "\Settings.txt")
    4.         End If
    5.  
    6.         Dim oFile As FileStream
    7.         Dim oWrite As StreamWriter
    8.  
    9.         oFile = New FileStream(Application.StartupPath & "\Settings.txt", FileMode.Create, FileAccess.Write)
    10.         oWrite = New StreamWriter(oFile)
    11.  
    12.         'oWrite.Write(Path) 'why write the file name in the file?
    13.         oWrite.WriteLine(Interval)
    14.         'oWrite.WriteLine(Startup.ToString)
    15.  
    16.         oFile.Close()
    17.         oFile = Nothing
    18.         oWrite = Nothing
    19.     End Sub
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Writing to a file with a subroutine

    If you are saving program settings I am a great advocate of serialization. Rather than writing to a text file and parsing it each time you read it in, keep your settings in a custom class and serialize to an XML file to save and deserialize to load. You don't need to worry about checking for a file and deleting as serialization can automatically overwrite an existing file.

    If you want to use a text file, I'm not sure why you are using a FileStream and a StreamWriter. You can create a new StreamWriter with a file name so there is no need for the FileStream in your code.

    Also, perhaps you should give a better indication of what is going wrong. Does your code generate an exception? Does it complete but the file is not created? Is the file created but no data written? How do we know if you don't tell us?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width