How do i launch this from resourses

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
6 posts Page 1 of 1
Contributors
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

i need to know a way to know the location of a resourses file invb.net see i have a .bat file that needs to take something from a resorse file and launch it but how do i do that or do i need to right them all to a file in appdata :P

need to know this ASAP please all help is apreashated
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Make sure that the resource is an "EMBEDDED RESOURCE" in the reources properties menu.
Right-Click the resource , choose Proerties, Build Type I think.. choose EMBEDDED RESOURCE.
Code: Select all
    Public Sub SaveToDisk(ByVal resourceName As String, ByVal fileName As String)
        Dim assy As [Assembly] = [Assembly].GetExecutingAssembly()
        For Each resource As String In assy.GetManifestResourceNames()
            If resource.ToLower().IndexOf(resourceName.ToLower) <> -1 Then
                Using resourceStream As System.IO.Stream = assy.GetManifestResourceStream(resource)
                    If resourceStream IsNot Nothing Then
                        Using reader As New BinaryReader(resourceStream)
                            Dim buffer() As Byte = reader.ReadBytes(CInt(resourceStream.Length))
                            Using outputStream As New FileStream(fileName, FileMode.Create)
                                Using writer As New BinaryWriter(outputStream)
                                    writer.Write(buffer)
                                End Using
                            End Using
                        End Using
                    End If
                End Using
                Exit For
            End If
        Next resource
    End Sub

this will save the resources as a file to your drive for local access
To Use:
Code: Select all
    SaveToDisk(My.Resources.RESOURCEFILE, Drive:\\PATH\FILENAME.EXT)
'or 
    SaveToDisk(My.Resources.RESOURCEFILE,Application.StartUpPath & "\filename.ext")


then you just do a simple process.start()
Code: Select all
    Process.start("path/to/saved_resource.ext") ' or the .BAT file you was talking about.
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Ive done this but when i go to debug and try it says the files does not exist and is not their i used it like this is that right
Code: Select all
    Public Sub SaveToDisk(ByVal resourceName As String, ByVal fileName As String)
        Dim assy As [Assembly] = [Assembly].GetExecutingAssembly()
        For Each resource As String In assy.GetManifestResourceNames()
            If resource.ToLower().IndexOf(resourceName.ToLower) <> -1 Then
                Using resourceStream As System.IO.Stream = assy.GetManifestResourceStream(resource)
                    If resourceStream IsNot Nothing Then
                        Using reader As New BinaryReader(resourceStream)
                            Dim buffer() As Byte = reader.ReadBytes(CInt(resourceStream.Length))
                            Using outputStream As New FileStream(fileName, FileMode.Create)
                                Using writer As New BinaryWriter(outputStream)
                                    writer.Write(buffer)
                                End Using
                            End Using
                        End Using
                    End If
                End Using
                Exit For
            End If
        Next resource
    End Sub

    Private Sub CrystalClearButton9_Click(sender As Object, e As EventArgs) Handles CrystalClearButton9.Click
        SaveToDisk(My.Resources.booter, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\booter.bat")
        Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\booter.bat")
    End Sub
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

for some reason, the code can not find the exstracted resource file.
did you set the resource file as an EMBEDDED RESOURCE in the properties menu?
Image
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Yes I did
Does it need to run as admin?
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

I can confirm the above code works if you are trying to extract a named embedded resource file from the project.

Though it would appear the first parameter of the SaveToDisk method takes the file name, not the resource contents.

You shouldn't need to run it as admin either.
6 posts Page 1 of 1
Return to “Coding Help & Support”