Link to home
Start Free TrialLog in
Avatar of Juan Velasquez
Juan VelasquezFlag for United States of America

asked on

Can't extract excel file from resources folder

Hello,
I am trying to extract an excel template that I have added to the resource folder of the project.  The code below (translated from portuguese) is designed to extract a file, in this case a template, and write it to a temp folder on the users machine.  However when I run the code, I can't find the file.  I understand most of the code and I think the problem isin
 Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly
 Assemblies are new to me so I'd like get some information on what they are.  I have a hazy notion of what they do but I need more information
Private Function GetResourceFile(ByVal fileName As String) As String

        Try

            'Create a name / temporary location for the file. By default it creates a
            ' file with extension *. tmp, and so you need to change the *. exe
            Dim tempPath As String = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".xltx")

            ' Checks the application name (Assembly) 
            Dim currentAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
            'MessageBox.Show(currentAssembly.GetName.ToString)
            ' Verify 
            Dim arrResources As String() = currentAssembly.GetManifestResourceNames()
            MessageBox.Show(currentAssembly.GetManifestResourceNames.Count.ToString())


            For Each resource As String In arrResources

                'Check if the resource has the name of the file to extract
                If resource.Contains(fileName) Then

                    ' Reads the resources of the executable to a Stream
                    Using resourceStream As IO.Stream = currentAssembly.GetManifestResourceStream(resource)

                        'Creates a new FileStream that will write the final file
                        Using writer As New IO.FileStream(tempPath, IO.FileMode.Create, IO.FileAccess.Write)

                            Const size As Int16 = 4096
                            Dim bytes(size) As Byte
                            Dim numBytes As Int32 = 0

                            'Write all bytes of Stream created using
                            'The FileStream and the method Write () on a loop
                            Do
                                numBytes = resourceStream.Read(bytes, 0, size)
                                writer.Write(bytes, 0, numBytes)

                            Loop While (numBytes > 0)

                        End Using ' writer

                    End Using ' resourceStream

                    ' Returns the file location
                    Return tempPath
                
                End If

            Next

            ' If you have not found the desired file
            Return String.Empty

        Catch ex As Exception
            Return String.Empty
        End Try

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Juan Velasquez
Juan Velasquez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial