gacutil /i Base64Codec.dll
Figure 15-35
If you are using Windows Vista, make sure to run the command prompt as Administrator.
If the installation is successful, you will see the shared assembly in the Assembly Cache Viewer (see Figure 15-36).
Figure 15-36
The version number displayed next to the DLL is specified by using the AssemblyVersion
attribute in the AssemblyInfo.cs
file (as discussed earlier). Select the Base64Codec DLL, and click the Properties button (the button with the tick icon) to see the Properties page as shown in Figure 15-37.
Figure 15-37
The version number displayed in this page is specified using the AssemblyFileVersion
attribute.
To install different versions of the same assembly to the GAC, simply modify the version number in AssemblyInfo.cs
(via the AssemblyVersion
attribute), recompile the assembly, and install it into the GAC.
Physically, the shared assembly is copied to a folder located under the GAC_MSIL
subfolder of the GAC, in the following format:
\assembly\GAC_MSIL\\_
In this example, it is located in:
C:\Windows\assembly\GAC_MSIL\Base64Codec\1.0.0.0_2a7dec4fb0bb6
Figure 15-38 shows the physical location of the Base64Codec.dll
assembly.
Figure 15-38
Making the Shared Assembly Visible in Visual Studio
By default, adding a shared assembly into the GAC does not make it appear automatically in Visual Studio's Add Reference dialog. You need to add a registry key for that to happen. Here's how to handle that.
First, launch the registry editor by typing regedit
in the Run command box.
If you are using Windows Vista, make sure to run regedit as Administrator.
Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders
key. Right-click on the AssemblyFolders key and select New→Key (see Figure 15-39).
Figure 15-39
Name the new key Base64Codec
. Double-click on the key's (Default) value, and enter the full path of the shared assembly (for example, C:\Documents and Settings\Wei-Meng Lee\My Documents\Visual Studio 2008\Projects\Base64Codec\bin\Debug
; see Figure 15-40).
Figure 15-40
Then restart Visual Studio 2008, and the assembly should appear in the Add Reference dialog.
Using the Shared Assembly
Let's now create a new Windows application project to use the shared assembly stored in the GAC. Name the project WinBase64
.
To use the shared assembly, add a reference to the DLL. In the Add Reference dialog, select the Base64Codec
assembly, as shown in Figure 15-41, and click OK.
Figure 15-41
Note in the Properties window that the Copy Local property of the Base64Codec is set to False (see Figure 15-42), indicating that the assembly is in the GAC.
Figure 15-42
Populate the default Form1
with the controls shown in Figure 15-43 (load the pictureBox1
with a JPG image).
Figure 15-43
In the code-behind of Form1
, define the two helper functions as follows:
Remember to import the System.IO
namespace for these two helper functions.
public byte[] ImageToByteArray(Image img) {
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
public Image ByteArrayToImage(byte[] data) {
MemoryStream ms = new MemoryStream(data);
Image img = new Bitmap(ms);
return img;
}
Code the Test button as follows:
private void btnTest_Click(object sender, EventArgs e) {
//---create an instance of the Helper class---
Base64Codec.Helper codec = new Base64Codec.Helper();
//---convert the image in pictureBox1 to base64---
string base64string =
codec.Encode(ImageToByteArray(pictureBox1.Image));
//---decode the base64 to binary and display in pictureBox2---
pictureBox2.Image = ByteArrayToImage(codec.Decode(base64string));
}
Here you are creating an instance of the Helper
class defined in the shared assembly. To test that the methods defined in the Helper
class are working correctly, encode the image displayed in pictureBox1
to base64, decode it back to binary, and then display the image in pictureBox2
.
Press F5 to test the application. When you click the Test button, an identical image should appear on the right (see Figure 15-44).
Figure 15-44
Examine the manifest of the WinBase64.exe
assembly to see the reference to the Base64Codec assembly (see Figure 15-45). Observe the public key token stored in the manifest — it is the public key token of the shared assembly.
Figure 15-45
Читать дальше