2

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.

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51
  • Any other way of reaching my goal (a one-click setup of the working environment) is of course also accepted :) – Saaru Lindestøkke May 16 '12 at 07:24
  • Do you just need to know how to read the embedded resource? Maybe this will help: http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – McGarnagle May 16 '12 at 08:53
  • No, I don't need to read an embedded resource in my code. When the vb app is run the files should be copied to the current directory according to the directory structure shown in the question. The answer you provided is about accessing an embedded resource in the code. – Saaru Lindestøkke May 25 '12 at 02:18
  • Something like this used to be common for many many years, but nowadays Windows does not like it when programs and data are mixed in the same folder structure. You should really try to follow best practices or you will have nothing but trouble. That said, is there any reason why you are not using a Setup project type in Visual Studio? – cdonner May 25 '12 at 02:34
  • I did not try a setup project yet. I was not aware of the existance, so not a good reason to not use it. – Saaru Lindestøkke May 25 '12 at 02:41

1 Answers1

2

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.

Bob Mc
  • 1,980
  • 1
  • 28
  • 38
  • Thanks for the elaborate answer, I'll assign the bounty as soon as possible. I'll try now to set-up an installation program as that seems to be the way to go. – Saaru Lindestøkke May 25 '12 at 05:53
  • Thank Bart. As per SO's policies (http://blog.stackoverflow.com/2011/01/the-wikipedia-of-long-tail-programming-questions/) I try to provide as complete an answer as possible. Even if it isn't the answer you're looking for it might answer *someone else's* problem during a site search. – Bob Mc May 25 '12 at 12:48