Send a string directly to a file

  • Thread starter Thread starter Greg Chu
  • Start date Start date
G

Greg Chu

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

Thanks!

Greg
 
I have been using these for a while... May fit the bill:

Public Function FILE_ReadIntoString(ByVal FileNameAndPath As String) As
String

Try

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

myStream.Close()

Else

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

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.

GC.Collect()

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

Try

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)

myFileStream.Close()

FILE_WriteFile = True

Catch ex As Exception

FILE_WriteFile = False

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

End Try

End Function
 
Greg Chu said:
Hi, any way to send a string directly to a file on the hard drive?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top