470,626 Members | 2,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

Send a string directly to a file

Hi, any way to send a string directly to a file on the hard drive?


Nov 21 '05 #1
3 1092
I have been using these for a while... May fit the bill:

Public Function FILE_ReadIntoString(ByVal FileNameAndPath As String) As


Dim myfile As System.IO.File

Dim Line As String

'Dim myStream As System.IO.FileStream

If myfile.Exists(FileNameAndPath) = True Then

' Create a file to write to.

Dim myStream As System.IO.StreamReader = myfile.OpenText(FileNameAndPath)

FILE_ReadIntoString = myStream.ReadToEnd



App_Print("ERROR: FILE_ReadIntoString: Could not find file: " &

End If

'The problem is not so much loading files into strings as what happens next

'in the code. Adding this line should help .Net make room.


Catch ex As Exception

FILE_ReadIntoString = ""

App_Print("Error - FILE_ReadIntoString: " & ex.ToString)

End Try

End Function

Public Function FILE_WriteFile(ByVal OutputString As String, ByVal
OutputFileName As String) As Boolean


Dim myFile As System.IO.File

Dim myFileStream As System.IO.FileStream

Dim myByteArray As Byte()

Dim MyEncoder As New System.Text.ASCIIEncoding

myFileStream = myFile.Create(OutputFileName)

myByteArray = MyEncoder.GetBytes(OutputString)

myFileStream.Write(myByteArray, 0, myByteArray.Length)


FILE_WriteFile = True

Catch ex As Exception

FILE_WriteFile = False

App_Print("Error - FILE_WriteFile: " & ex.ToString)

End Try

End Function

"Greg Chu" <gr******@wendys.com> wrote in message
Hi, any way to send a string directly to a file on the hard drive?



Nov 21 '05 #2
"Greg Chu" <gr******@wendys.com> schrieb:
Hi, any way to send a string directly to a file on the hard drive?

Take a look at the 'System.IO.StreamWriter' class...

M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

The streamwriter/streamreader pages on MSDN are full of samples.

Here the streamwriter.


I hope this helps,

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by James Hong | last post: by
2 posts views Thread by Fatih BOY | last post: by
6 posts views Thread by ShieldsJared | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.