dotnet thoughts 

a dotnet developer's technical blog

Creating CAB files using MakeCab.exe

If you are working with Sharepoint or Infopath sometime you need to use makecab.exe, it is a command line utility to create cab files from Microsoft. You can use makecab utility from command prompt.

Command line output from MakeCab/?


MAKECAB [/V[n]] [/D var=value ...] [/L dir] source [destination]
MAKECAB [/V[n]] [/D var=value ...] /F directive_file [...]

source  - File to compress.
destination  - File name to give compressed file. If omitted, the
last character of the source file name is replaced
with an underscore (_) and used as the destination.
/F directives - A file with MakeCAB directives (may be repeated).
/D var=value - Defines variable with specified value.
/L dir - Location to place destination (default is current directory).
/V[n] - Verbosity level (1..3)

But I was unable to create the cab file with multiple files using this. After a long search I found one solution from MSDN – Using MakeCab.exe. Then I have created one .ddf file and using the command


makecab.exe /f Sample.ddf

And in the sample.ddf you can specify the file names.


;*** Sample Source Code MakeCAB Directive file example
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate="Sample.cab"
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1="Cabs"
image1.gif
image2.gif
mystyle.xsl
manifest.xsf
myschema.xsd
script.vbs
template.xml
upgrade.xsl
;*** <the end>

I think the code is self explanatory.

ObservableCollection Generic Class in .Net 2.0

Microsoft intrdouced a new collection class, in .Net which give you notifications(events), while items getting added, removed and refreshed. You can get more information about this class in this link ObservableCollection Generic Class. As per the MSDN documentation this class is available in System.Collections.ObjectModel,but I am unable to find this class in VS 2005 in .Net 2.0. So I created a class, to give me almost same functionality


Public Class ObserverCollection(Of T)
Inherits System.Collections.Generic.List(Of T)
Public Event ItemAdded(ByVal o As T)
Public Event ItemRemoved()
Public Overloads Sub Add(ByVal Value As T)
MyBase.Add(Value)
RaiseEvent ItemAdded(Value)
End Sub
Public Overloads Sub Remove(ByVal Value As T)
MyBase.Remove(Value)
RaiseEvent ItemRemoved()
End Sub
End Class

It is a simple implementation, where I am using the System.Collections.Generic.List(Of T) as my base class, and I am overloading the methods. In the overloaded methods, I am raising the events.

Update : This class not available on .Net Framework 2.0
Update 2: This class will available on Visual Studio 2008, only after adding reference to the WindowBase.dll, normally it will not come. You need to add reference explicitly