Search code examples
vb.netvisual-studio-2008embedded-resource

Let VB Form prepare working environment in chosen directory


I am working on a GUI for a simulation program. The simulation program is a single .exe which is driven by an input file (File.inp placed in the same directory). The Original.inp file functions as a template from which the form reads all the values into an array. Then it changes these values reflecting the changes done by the user in the form. After that it writes all the new values to File.inp. By pushing the "Run" button the Simulation.exe file is executed. The folder structure looks like this:

root  
|  
|---input   
|   |  
|   |--Original.inp
|
|---GUI.exe
|---Simulation.exe
|---File.inp

Ideally I would supply only the GUI, the user would select the working directory and then the GUI.exe would create an input folder and extract the Original.inp and Simulation.exe in the appropriate locations. So far I have only managed to include Original.inp and Simulation.exe as "EmbeddedResources" in my VB project and I have let my code create an input folder in the working directory chosen by the user.

Can someone please explain to me how I can extract the .inp and .exe file into the correct directories? I've searched on google, tried File.WriteAllBytes and Filestream.WriteByte but did not get the desired results.

The problem with File.WriteAllBytes was that I could not point to the embedded resource ("Simulation.exe is not a member of Resources" and with Filestream.WriteByte I got a 0 kb file.


Solution

  • The question commenters are correct, this is probably a task best left for a setup program. However, that having been stated, in the interest of answering the question as asked I offer the following approach.

    Contrary to your supposition in your question comment, you do need to "read" the embedded resource from the GUI's executable file, since it's an embedded resource and not an external resource. It won't magically extract itselt from the executable file. You need to do the manual read from the assembly and write to your specified locations. To do this, you need to read the resource using .Net Reflection, via the currently executing assembly's GetManifestResourceStream method.

    The Simulation.exe file is a binary file so it must be handled as such. I assumed that the Orginal.inp file was a text file since it afforded the opportunity to demonstrate different types of file reads and writes. Any error handling (and there should be plenty) is omitted for brevity.

    The code could look something like this:

    Imports System.IO
    Imports System.Reflection
    
    Module Module1
    
    Sub Main()
        'Determine where the GUI executable is located and save for later use
        Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
        Dim appFolder As String = Path.GetDirectoryName(thisAssembly.Location)
    
        Dim fileContents As String = String.Empty
    
        'Read the contents of the template file. It was assumed this is in text format so a 
        'StreamReader, adept at reading text files, was used to read the entire file into a string
        'N.B. The namespace that prefixes the file name in the next line is CRITICAL. An embedded resource
        'is placed in the executable with the namespace noted in the project file, so it must be 
        'dereferenced in the same manner.
        Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Original.inp")
            If fileStream IsNot Nothing Then
                Using textStreamReader As New StreamReader(fileStream)
                    fileContents = textStreamReader.ReadToEnd()
                    textStreamReader.Close()
                End Using
                fileStream.Close()
            End If
        End Using
    
        'Create the "input" subfolder if it doesn't already exist
        Dim inputFolder As String = Path.Combine(appFolder, "input")
        If Not Directory.Exists(inputFolder) Then
            Directory.CreateDirectory(inputFolder)
        End If
    
        'Write the contents of the resource read above to the input sub-folder
        Using writer As New StreamWriter(Path.Combine(inputFolder, "Original.inp"))
            writer.Write(fileContents)
            writer.Close()
        End Using
    
        'Now read the simulation executable. The same namespace issues noted above still apply.
        'Since this is a binary file we use a file stream to read into a byte buffer
        Dim buffer() As Byte = Nothing
        Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Simulation.exe")
            If fileStream IsNot Nothing Then
                ReDim buffer(fileStream.Length)
                fileStream.Read(buffer, 0, fileStream.Length)
                fileStream.Close()
            End If
        End Using
    
        'Now write the byte buffer with the contents of the executable file to the root folder
        If buffer IsNot Nothing Then
            Using exeStream As New FileStream(Path.Combine(appFolder, "Simulation.exe"), FileMode.Create, FileAccess.Write, FileShare.None)
                exeStream.Write(buffer, 0, buffer.Length)
                exeStream.Close()
            End Using
        End If
    
    End Sub
    
    End Module
    

    You will also have to add logic to determine if the files have been extracted so it doesn't happen every time the GUI is invoked. That's a big reason why an installation program might be the correct answer.