Trikks

The digital adventures of Eric Herlitz

Archive for November, 2011

Failure adding assembly to the cache: This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Posted by trikks on November 19, 2011

So you got the “Failure adding assembly to the cache: This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.” when running Gacutil

Well good news is that this is easy to fix, you are simply running an old version of gacutil

open a terminal en enter gacutil, maybe something like
PS C:\Windows\Microsoft.NET\Framework\v1.1.4322> .\gacutil.exe
Microsoft (R) .NET Global Assembly Cache Utility. Version 3.5.30729.1
Copyright (c) Microsoft Corporation. All rights reserved.
Check the version

Now this option may vary a bit, but in my case the gacutil I should use was in this folder

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64

PS C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64> gacutil
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.

So, by simply switching to this version my problems are solved!

Posted in .net | 10 Comments »

Compress a file using GZip and convert it to Base64 – and back – using C#

Posted by trikks on November 3, 2011

Sometimes you want to store a file in a database or even transfer stuff over the Internet through different protocols, maybe even to other platforms. In many of these cases the traditional methods simply wont do.

Storing files in databases is usually made by sending the file as a blob to the database and it usually works out pretty good to, but in some cases you simply want a different structure. I often tend to end up in projects with several platforms and languages, this means XML or similar languages like JSON. Anyway binary data is out, you need a different way to transfer or store files.

When it comes to files I recommend using GZip compression which is shipped as a standard with .NET. If you want more simple compression you should be looking at gzdeflate and gzinflate.

If you only want the source project download it here  http://www.trikks.com/download.php?id=CompressConsole.zip

Compressing a file

string fileName = @"c:\test.pdf";
byte[] file = File.ReadAllBytes(fileName);

// Compress
byte[] compress = Compress(file);
public static byte[] Compress(byte[] data)
{
    using (var compressedStream = new MemoryStream())
    using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
    {
        zipStream.Write(data, 0, data.Length);
        zipStream.Close();
        return compressedStream.ToArray();
    }
}

Encode the byte array

string encoded = base64_encode(compress);
public string base64_encode(byte[] data)
{
	if (data == null)
		throw new ArgumentNullException("data");
	return Convert.ToBase64String(data);
}

The contents of “encoded” is your file but compressed and base64 encoded. You can take this string and send it through any protocol allowing strings.

If we print the encoded string it looks something like this

Decode and Decompress

// Decode and decompress
byte[] decoded = base64_decode(encoded);
byte[] decompressed = Decompress(decoded);
File.WriteAllBytes(@"c:\out.pdf", decompressed);
public static byte[] base64_decode(string encodedData)
{
    byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
    return encodedDataAsBytes;
}
public static byte[] Decompress(byte[] data)
{
    using (var compressedStream = new MemoryStream(data))
    using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    using (var resultStream = new MemoryStream())
    {
        var buffer = new byte[4096];
        int read;

        while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            resultStream.Write(buffer, 0, read);
        }

        return resultStream.ToArray();
    }
}

Now that wasn’t hard was it :)

You can download a visual studio project here http://www.trikks.com/download.php?id=CompressConsole.zip

Posted in C# | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.