[RESOLVED] Write a string to a filestream?
My goal is to upload a string to a txt file on an ftp server.
Here is what I have found so far:
vb Code:
Dim clsRequest As System.Net.FtpWebRequest = _
DirectCast(System.Net.WebRequest.Create(RemoteFilename), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("usr", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
'write the string to a local text file
Dim Report as String = "Test Report"
Dim ReportFile As String = "C:\report.txt"
Dim strWriter As IO.StreamWriter
strWriter = New IO.StreamWriter(ReportFile)
strWriter.Write(Report)
strWriter.Close()
' read in file
Dim bFile() As Byte = System.IO.File.ReadAllBytes(ReportFile)
' upload file
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
It seems pretty silly to do it like this! (write the file and then immediately read it again). Is there a way to convert a String into something that clsStream.Write will accept?
Thanks,
Dave
Re: Write a string to a filestream?
Cant you just do:
vb Code:
System.Text.Encoding.ASCII.GetBytes("Some string here")
or does that not work in this case? (I've not played with streams and bytes much to be honest so I'm kind of guessing there...)
Re: Write a string to a filestream?
It works just fine, I just didn't know about that :)
Thanks,
Dave
Re: [RESOLVED] Write a string to a filestream?
Cool, glad to hear it works :)