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
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:
Public Sub SaveSettings(ByVal Path As String, ByVal Interval As Integer, ByVal Startup As Boolean)
If File.Exists(Application.StartupPath & "\Settings.txt") Then
File.Delete(Application.StartupPath & "\Settings.txt")
End If
Dim oFile As FileStream
Dim oWrite As StreamWriter
oFile = New FileStream(Application.StartupPath & "\Settings.txt", FileMode.Create, FileAccess.Write)
oWrite = New StreamWriter(oFile)
'oWrite.Write(Path) 'why write the file name in the file?
oWrite.WriteLine(Interval)
'oWrite.WriteLine(Startup.ToString)
oFile.Close()
oFile = Nothing
oWrite = Nothing
End Sub
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?