Results 1 to 4 of 4

Thread: [RESOLVED] Write a string to a filestream?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Resolved [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:
    1. Dim clsRequest As System.Net.FtpWebRequest = _
    2.             DirectCast(System.Net.WebRequest.Create(RemoteFilename), System.Net.FtpWebRequest)
    3.         clsRequest.Credentials = New System.Net.NetworkCredential("usr", "pw")
    4.         clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    5.  
    6. 'write the string to a local text file
    7.         Dim Report as String = "Test Report"
    8.         Dim ReportFile As String = "C:\report.txt"
    9.         Dim strWriter As IO.StreamWriter
    10.         strWriter = New IO.StreamWriter(ReportFile)
    11.         strWriter.Write(Report)
    12.         strWriter.Close()
    13.  
    14.         ' read in file
    15.         Dim bFile() As Byte = System.IO.File.ReadAllBytes(ReportFile)
    16.  
    17.         ' upload file
    18.         Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
    19.         clsStream.Write(bFile, 0, bFile.Length)
    20.         clsStream.Close()
    21.         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

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Write a string to a filestream?

    Cant you just do:
    vb Code:
    1. 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...)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Re: Write a string to a filestream?

    It works just fine, I just didn't know about that

    Thanks,

    Dave

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Write a string to a filestream?

    Cool, glad to hear it works
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width