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
From novice to tech pro — start learning today.