Memory Stream

Posted on 2011-10-11
Last Modified: 2012-05-12
I am saving a file in the database, I need to be able to allow the user to edit the file then save it back away.

I also need to know how to launch a file from a memory stream

This allows the user to save the memory file to a location, I really need an edit option

     Using ms = New MemoryStream(_document.MergeDocument, False)

            Using fs = File.Open(String.Format("{0){1}", Path.GetTempPath, uteFilename.Text), FileMode.Create, FileAccess.Write)
            End Using

        End Using
Question by:rocky050371
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Edit it how? You don't have to write out the MemoryStream to a file in order to be able to edit it. If the data is small enough, you could keep the data in a string variable and edit that, writing the changes back to the DB when finished.

    As far as "to launch a file from a memory stream," if you mean you have an executable in the MemoryStream (i.e. binary data), you could write the data out to a temporary file and then use Process.Start to execute it. There are probably ways to execute without writing out to a file, but IMO this would be the simplest way to execute.
    LVL 17

    Accepted Solution

    Hello, I did this example based on what I think you are asking for, this will take your file data and then open the default program to edit it, when the user closes the program it will continue to allow you save the edited file:
    Sub EditMyFile(ByVal myFileName As String, ByVal myFileData As Byte())
        ' The file extension is required to be able to start the default program to edit that kind of file.
        Dim fileExt As String = System.IO.Path.GetExtension(myFileName)
        ' Make a path for a temporary file
        Dim myTempFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString() + fileExt)
        ' This will contain the edited file when the user close the program that have used to edit it
        Dim editedFileData As Byte()
            System.IO.File.WriteAllBytes(myTempFilePath, myFileData)
            Using myProcess As System.Diagnostics.Process = Process.Start(myTempFilePath)
                ' Wait for the user to close the program (editor) before continue
            End Using
            ' Take the edit file data
            editedFileData = System.IO.File.ReadAllBytes(myTempFilePath)
            ' Clean up
        End Try
        ' Save back your edited data to the DB by using editedFileData
        'etc etc etc...
    End Sub

    Open in new window

    How to use? example:
    Private Sub Test()
        EditMyFile("c:\temp\Hi.doc", System.IO.File.ReadAllBytes("c:\temp\Hi.doc"))
    End Sub

    Open in new window

    LVL 83

    Expert Comment

    And if you want to allow a third party program such as MS Word to modify the file and save it back then you would need to do some work. Word can not open the file from memory stream within your program. You would need to save the file to disk, launch the program, monitor it, and when program closes, see if changes have been made and update the file in DB.
    LVL 17

    Expert Comment

    by:Carlos Villegas
    To be a little more clear I think:
    Private Sub Test()
        EditMyFile("Hi.doc", MyFileByteArray)
    End Sub

    Open in new window

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Based in your example, maybe you can use the EditMyFile method in this way:
    Private Sub Test()
        EditMyFile(uteFilename.Text, _document.MergeDocument)
    End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now