|
-
Jun 5th, 2005, 02:47 AM
#1
Thread Starter
New Member
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
-
Jun 5th, 2005, 03:12 AM
#2
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
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
-
Jun 5th, 2005, 07:43 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|