locked
Change content of Embedded file RRS feed

  • Question

  • Hey

    I use Vb.NET from VS 2010

    I use a SQL File to run a script, when changes to the database should be done.
    This works fine for me. The SQL File is in my project and marked as embedded file.

    Now I run into an issue. I have to change the user name, when updating a Trigger or SP (for complete information: I use mySQL and 3rd Party Script component)

    So, I thought, I write the embedded file into a Temp-File, make the changes to the file, run the temp file through the script component and then delete the file.

    Sounds quite easy... But I have no idea how to do this...
    Please, can anyone help me doing this.

    THX a lot

    Saturday, December 4, 2010 11:26 AM

Answers

  • If you want to extract the file from embedded resources and save it disk, then try the code below

    Public Sub SaveToDisk(ByVal resourceName As String, ByVal fileName As String)
        ' Get a reference to the running application.
        Dim assy As Assembly = Assembly.GetExecutingAssembly()
        Dim MyNamespace As String = assy.GetName().Name.ToString()
        ' Loop through each resource, looking for the image name (case-insensitive). 
        ' Get the embedded file from the assembly as a MemoryStream. 
        Try
          Using resourceStream As System.IO.Stream = assy.GetManifestResourceStream(MyNamespace & "." & resourceName)
            If resourceStream IsNot Nothing Then
              Using fs As New FileStream(fileName, FileMode.OpenOrCreate)
                ' Read the bytes from the input stream.
                For x As Integer = 1 To resourceStream.Length
                  fs.WriteByte(resourceStream.ReadByte)
                Next
              End Using
            End If
          End Using
        Catch nu As NullReferenceException
        Catch ex As Exception
    
        End Try
    
      End Sub
    
    
    '=========USAGE=============
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
     'kaymaf.exe is the file to extract from embedded resources and save to C drive   
     SaveToDisk("kaymaf.exe", "C:\kaymaf.exe")
     MsgBox("Done")
     End Sub
    
    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by Zero-G. _ Wednesday, December 8, 2010 3:05 PM
    Saturday, December 4, 2010 4:16 PM

All replies

  • If you want to extract the file from embedded resources and save it disk, then try the code below

    Public Sub SaveToDisk(ByVal resourceName As String, ByVal fileName As String)
        ' Get a reference to the running application.
        Dim assy As Assembly = Assembly.GetExecutingAssembly()
        Dim MyNamespace As String = assy.GetName().Name.ToString()
        ' Loop through each resource, looking for the image name (case-insensitive). 
        ' Get the embedded file from the assembly as a MemoryStream. 
        Try
          Using resourceStream As System.IO.Stream = assy.GetManifestResourceStream(MyNamespace & "." & resourceName)
            If resourceStream IsNot Nothing Then
              Using fs As New FileStream(fileName, FileMode.OpenOrCreate)
                ' Read the bytes from the input stream.
                For x As Integer = 1 To resourceStream.Length
                  fs.WriteByte(resourceStream.ReadByte)
                Next
              End Using
            End If
          End Using
        Catch nu As NullReferenceException
        Catch ex As Exception
    
        End Try
    
      End Sub
    
    
    '=========USAGE=============
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
     'kaymaf.exe is the file to extract from embedded resources and save to C drive   
     SaveToDisk("kaymaf.exe", "C:\kaymaf.exe")
     MsgBox("Done")
     End Sub
    
    kaymaf

    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by Zero-G. _ Wednesday, December 8, 2010 3:05 PM
    Saturday, December 4, 2010 4:16 PM
  • Hey

     

    Sorry for the late reply!

    THX! - That was, what I have looked for!

    Have a nice day!

    Wednesday, December 8, 2010 3:05 PM