PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
StreamWriter / Memory based txt file-VBForums
Results 1 to 9 of 9

Thread: StreamWriter / Memory based txt file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    687

    StreamWriter / Memory based txt file

    Looking for a quick way to send a .txt file to ftp without putting the file on disk. Would like to send it right out of memory and dispose it. My StreamWriter skills only take me so far.

    Code:
            Dim FullFilePath As String = "C:\DATA\" & FileName & ".txt" 'Becomes irrelevant
    
            File.Create(FullFilePath).Dispose() 'Dont want this step
    
            Dim SW As StreamWriter = New StreamWriter(FullFilePath) 'Want to write to memory, probably a byte array
            SW.WriteLine(StrBldr.ToString) 'Text is from stringbuilder output
            SW.Close()
            
             'Skipping ahead, "FullFilePath" SHould be replace with output from StreamWriter
             session.PutFiles(FullFilePath, "/Upload/Users/mft/", False, transferOptions)

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,634

    Re: StreamWriter / Memory based txt file

    A StreamWriter is an object that writes text to a Stream. There is a constructor that takes a String as an argument that will assume that that String contains a file path and create a FileStream internally. There is another constructor that takes a Stream as an argument. That can be any type of Stream, including a FileStream, a NetworkStream or a MemoryStream. If you want basically the contents of a text file in memory but not on disk then you can create a MemoryStream and write to that using a StreamWriter, e.g.
    vb.net Code:
    1. Using stream As New MemoryStream
    2.     Using writer As New StreamWriter(stream)
    3.         writer.Write(someText)
    4.     End Using
    5.  
    6.     'stream can now be used anywhere a Stream is required.
    7. End Using
    That's how to get your data in memory. If you can then send that to an FTP folder and how that would be done depends on the object referred to by your 'session' variable. I have no idea what that is so I have no idea whether it can accept a Stream or not. That's for you to find out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    465

    Re: StreamWriter / Memory based txt file

    On the off chance that you are using WinSCP (which comes up first when searching for session.PutFiles), then the documentation linked to below states that the first parameter is a String that points to a path containing the file(s) to upload.

    https://winscp.net/eng/docs/library_session_putfiles

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    687

    Re: StreamWriter / Memory based txt file

    Quote Originally Posted by OptionBase1 View Post
    On the off chance that you are using WinSCP (which comes up first when searching for session.PutFiles), then the documentation linked to below states that the first parameter is a String that points to a path containing the file(s) to upload.

    https://winscp.net/eng/docs/library_session_putfiles
    Yes, it is indeed a path to a file on disk.

  5. #5
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    465

    Re: StreamWriter / Memory based txt file

    So, just to clarify, you are using WinSCP, yes? And if so, you are aware that the documentation specifically states that the .PutFiles method receives a String that contains a path to the file(s) to upload, yes?

    So, is your question then if you can hold a file in memory but give it some virtual path that can function just like a real path to a real file on the disk, but that path will not really exist on the disk, but will instead point to the file in memory?

    If that is your question, I wish you good luck.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,634

    Re: StreamWriter / Memory based txt file

    There is such a thing as a MemoryMappedFile but I don't see how it can help you here because you cannot refer to them with a standard file path. That PutFiles method is almost certainly going to create a FileStream using the path you pass it so that path must represent something that a FileStream can be created from, i.e. a file on disk. A FileStream can be created from a SafeFileHandle, although that PutFiles method doesn't accept one, but you can't even get one of those form a MemoryMappedFile anyway. You can get a SafeMemoryMappedFileHandle and it has the same base class as SafeFileHandle but neither inherits the other. Basically, you're limited by what that 'sessions' object can do and it appears that it can only read a file from disk by path.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    687

    Re: StreamWriter / Memory based txt file

    After googling some different keywords sparked from this thread I see it's not possible
    https://winscp.net/forum/viewtopic.php?t=14993

    Which means I got to bitlock some machines, which sucks.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,655

    Re: StreamWriter / Memory based txt file

    I assume if you opened your own TCP connection to the FTP server and your code acted as an FTP client, you could send whatever you wanted without having to write anything to Disk.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,634

    Re: StreamWriter / Memory based txt file

    Quote Originally Posted by passel View Post
    I assume if you opened your own TCP connection to the FTP server and your code acted as an FTP client, you could send whatever you wanted without having to write anything to Disk.
    Quite so. WebClient.UploadData is a simple way to upload a Byte array directly to a URI, which should would with an FTP URL I believe.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width