View Full Version : Saving
El Barto
09-21-04, 11:09 AM
'Ello,
I am trying to make a porgram which is all going fine atm, but I started thinking about saving it, and the fact that I have no idea how to. When I say saving it I mean when the user runs the program and then does what they are doing I need it to save to either a file or to a database, can any one help me out? All the data the user enters i stored in variables.
Hi,
Here is a function for saving to a text file:
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
And here is another function for reading text from a text file:
Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function
Here is how you use the two functions. If you don't want to include error retrieval, delete the Debug.WriteLine() statements and the corresponding if statements :
sContents = GetFileContents("C:\test2.txt", sErr)
If sErr = "" Then
Debug.WriteLine("File Contents: " & sContents)
'Save to different file
bAns = SaveTextToFile(sContents, "C:\test2.txt", sErr)
If bAns Then
Debug.WriteLine("File Saved!")
Else
Debug.WriteLine("Error Saving File: " & sErr)
End If
Else
Debug.WriteLine("Error retrieving file: " & sErr)
End If
El Barto
09-23-04, 12:05 PM
Thank you :)
TLWallace
10-10-04, 11:31 PM
The more "Elegant" way of doing what you're trying is with XML:
//**************************************
//
// Name: XML Writer to String
// Description:Converts an XMLWriter str
// eam into a string so you dont have to du
// mp the XMLWriter stream to a temp file.
// By: Bryan A Cairns
//
//This code is copyrighted and has
// limited warranties.Please see
http://www.Planet-Source-Code.com/vb/scripts/Sh (http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2302&lngWId=10)owCode.asp?txtCodeId=2302&lngWId=10
//for details.
//************************************** //
Imports System.Xml
Imports System.IO
Imports System.Text.ASCIIEncoding
'use the function below as a test
Public Sub test()
MsgBox(CreateLoginPacket("a", "b", "c"))
End Sub
Public Function CreateLoginPacket(ByVal Username As String, ByVal Password (http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=2302&lngWId=10#) As String, ByVal Role As String) As String
Dim data() As Byte
Dim sData As String
Dim ms As MemoryStream = New MemoryStream()
Dim XW As New XmlTextWriter(ms, System.Text.Encoding.UTF8)
With XW
.WriteStartDocument(True)
.WriteStartElement("Packet")
.WriteElementString("Type", "Login")
.WriteElementString("Username", Username)
.WriteElementString("Password", Password)
.WriteElementString("Role", Role)
.WriteEndElement()
.WriteEndDocument()
.Flush()
ms.Seek(0, SeekOrigin.Begin)
sData = ASCII.GetChars(ms.ToArray)
.Close()
End With
Return sData
End Function
elnerdo
02-02-05, 05:00 PM
What makes that any more elegant?
I personally prefer to use the streamwriters and readers.
A more "Elegant" way TLWallace? :eek:
Correct me if I am wrong, but we were not talking about XML. The question was about readind and writing string to a simple text file. :rolleyes:
rdprogrammer
02-03-05, 01:43 PM
What makes that any more elegant?
I personally prefer to use the streamwriters and readers.
elnerdo, you have no right no blow off the ways of other programmers, what makes you think you are better? i would like for somone to propose a project idea that cold be completed in under a week, and you and i will have alittle competition to see who can write it first, i'll show you that you have no right to insult other programmers.
richard
PS any takers for judge?
I don't think "elnerdo" meant it as an insult. I think he has a valid point, since the original questions was about a simple string read and write function.
Also, a competition? .... lol
Put your time and energy towards more productive coding then competing with others for nothing. :D
elnerdo
02-06-05, 04:06 PM
Little hostile there Richard?
I was only saying that because it seemed a lot simpler to do it with normal streamreaders and writers. They're SPECIFICALLY built to write and read textfiles so they seem correct for the job.
I'm not trying to bash Tlwallace in any way at all. Hell, I have NO idea even the most BASIC XML. I barely know what Xml does, he's most likely far better than me.
Offtopic: Though this was off topic, I'll reply anyway: A contest seems like a lot of fun, but I just got back from vacation today and I missed a lot of school work and have a crapload of homework to do, as long as the contest would start next week, I'd be all right with it.
Is this about my reply to your fix the flickering post? I wasn't trying to make you seem foolish, just your way doesn't work. If this is true then: According to your profile, you're almost twenty years old, it's pathetic that someone that old could be so immature. If this is not true then: You've got some serious problems, taking offense to things that don't even pertain to you at all. Lighten up. I'm going to stop now before this turns into a flame battle and we're both banned. I suggest you do too.
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.