首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让VB窗体在所选目录中准备工作环境

让VB窗体在所选目录中准备工作环境
EN

Stack Overflow用户
提问于 2012-05-16 14:21:09
回答 1查看 301关注 0票数 2

我正在为一个模拟程序开发图形用户界面。模拟程序是由输入文件(File.inp放在同一目录中)驱动的单个.exe。Original.inp文件用作模板,表单从该模板中将所有值读取到一个数组中。然后,它更改这些值,以反映用户在表单中所做的更改。在此之后,它将所有新值写入File.inp。通过按下"Run“按钮,将执行Simulation.exe文件。文件夹结构如下所示:

代码语言:javascript
复制
root  
|  
|---input   
|   |  
|   |--Original.inp
|
|---GUI.exe
|---Simulation.exe
|---File.inp

理想情况下,我只提供图形用户界面,用户将选择工作目录,然后GUI.exe将创建一个输入文件夹,并将Original.inpSimulation.exe提取到适当的位置。到目前为止,我只设法将Original.inpSimulation.exe作为"EmbeddedResources“包含在我的VB项目中,并且我已经让我的代码在用户选择的工作目录中创建了一个输入文件夹。

谁能给我解释一下如何将.inp和.exe文件解压到正确的目录中?我在谷歌上搜索了一下,尝试了File.WriteAllBytesFilestream.WriteByte,但没有得到想要的结果。

File.WriteAllBytes的问题是我不能指向嵌入的资源("Simulation.exe不是资源的成员“,而使用Filestream.WriteByte我得到了一个0kb的文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-25 11:25:39

问题评论者是正确的,这可能是一个最好的任务留给安装程序。然而,如上所述,为了回答所提出的问题,我提供了以下方法。

与您在问题注释中的假设相反,您确实需要从GUI的可执行文件中“读取”嵌入式资源,因为它是嵌入式资源,而不是外部资源。它不会神奇地从可执行文件中提取自身。您需要手动读取程序集并写入您指定的位置。为此,您需要通过当前正在执行的程序集的GetManifestResourceStream方法,使用.Net反射读取资源。

Simulation.exe文件是二进制文件,因此必须这样处理。我假设Orginal.inp文件是一个文本文件,因为它提供了演示不同类型的文件读写的机会。为简洁起见,省略了任何错误处理(应该有很多错误处理)。

代码可能如下所示:

代码语言:javascript
复制
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

您还必须添加逻辑来确定文件是否已解压缩,这样就不会在每次调用GUI时都发生这种情况。这就是为什么安装程序可能是正确答案的一个重要原因。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10613051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档