Home > OS >  (VB.NET) Issues Adding or Extracting .cab Files as Resource Files
(VB.NET) Issues Adding or Extracting .cab Files as Resource Files

Time:06-14

I am working with VB.NET and cannot seem to add .cab files to my resources properly? Or maybe extraction is my issue?

I can add single files fine such as .bat and .txt. I go through the same process and even see the Cab files in resources with the other files. I set them up as Content and Copy Always like the others. But when I extract all the resource files with my standalone EXE, the Cab files output as just empty file. Any other single resource file extracts fine.

If it is my extraction process, my code is below:

    Private Sub CreateFile(ByVal Resource As Object, ByVal FullPath As String)
        Dim TheFile As StreamWriter
        Try
            TheFile = New StreamWriter(FullPath)
            TheFile.Write(Resource)
            TheFile.Close()
        Catch
        End Try
    End Sub

I was under the impression .cab files were actual files unless they are seen as compressed folders by windows? I originally had a .ZIP with sub folders that I successfully converted to a .CAB so my EXE can deploy it with the folder/directory structure intact. On my coding PC I can manually expand the CAB and the file structure is intact. There are about 4-5 files per sub-folder and 3 sub-folders with a readme.txt at the main folder level.

Maybe .cab is not the best way to go? Any other ways to get a .zip of folders and files into a resource in VB.NET if .CAB is not the way? Or maybe I am missing a module or something else that could help?

CodePudding user response:

The solution was quite easy actually, thanks to John's comment above regarding File.WriteAllBytes

My.Computer.FileSystem.WriteAllBytes(FilePathWithFileName, _
     My.Resources.YourResourceName, False)

Which allowed me to create the .CAB files perfectly.

In case anyone is having a similar issue, and needs the code to then expand a CAB, that was easy too:

Dim ExtractCAB As New ProcessStartInfo("Expand", _
     """FilePathWithFileName"" -F:* ""ExtractFilePath""") _
     With {.WindowStyle = ProcessWindowStyle.Hidden, .Verb = "runas"}

Process.Start(ExtractCAB)
  • Related