[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Memory Stream

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)
                ms.WriteTo(fs)
            End Using

        End Using
0
rocky050371
Asked:
rocky050371
  • 3
1 Solution
 
käµfm³d 👽Commented:
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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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()
    Try
        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
            myProcess.WaitForExit()
        End Using
    Finally
        ' Take the edit file data
        editedFileData = System.IO.File.ReadAllBytes(myTempFilePath)

        ' Clean up
        System.IO.File.Delete(myTempFilePath)
    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

0
 
CodeCruiserCommented:
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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
To be a little more clear I think:
Private Sub Test()
    EditMyFile("Hi.doc", MyFileByteArray)
End Sub

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now