Home > .NET > Unpacking the .NET Framework Source Code

Unpacking the .NET Framework Source Code

Microsoft "released" the source of the .NET framework a while ago. The details are here:

http://referencesource.microsoft.com/

But of course, it’s not that simple. You’re not supposed to be able to read the source willy-nilly; you’re only supposed to be able to see it when you’re debugging a program. So there’s some instructions to set it up with Visual Studio. I got this to work once, but since I applied Service Pack 1 it hasn’t worked – I just get blank files.

Then there’s this curious page:

http://referencesource.microsoft.com/netframework.aspx

As of today, it just has three Microsoft Installer packages that you can download, which have cryptic names. No indication of the significance of each package, or whether you need all of them. It all stinks of Microsoft grudgingly complying with the outcome of a legal dispute with the European Union, doesn’t it?

And if you download them and run them, it puts some intriguingly large files on your disk. They are all called source.zip.tmp. They are definitely not zip files.

But they do contain the source. And it turns out to be very easy to unpack them! Just use a program like this:

using System;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Please specify the input file and the output directory");
                return;
            }

            StreamReader reader = new StreamReader(args[0]);
            StreamWriter writer = null;

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] parts = line.Split(',');

                if (parts.Length == 4)
                {
                    int number;
                    if (int.TryParse(parts[0], out number))
                    {
                        if (parts[1].StartsWith("/"))
                        {
                            string path = Path.Combine(args[1], parts[1].Substring(1).Replace('/', '\\'));

                            // Hack-around for paths that are too long for the file system...
                            path = path.Replace("DEVDIV\\depot\\DevDiv\\releases\\", "");
                            
                            string dir = Path.GetDirectoryName(path);
                            if (!Directory.Exists(dir))
                                Directory.CreateDirectory(dir);

                            if (writer != null)
                                writer.Dispose();

                            writer = new StreamWriter(path);
                            continue;
                        }
                    }
                }

                Debug.Assert(writer != null);
                writer.WriteLine(line);
            }

            if (writer != null)
                writer.Dispose();
        }
    }
}

So I can only assume that Microsoft intended people to write the above program, or something similar. Judging from the EULA, unpacking the source, just so that you can search it and read it more conveniently, is perfectly okay.

It’s certainly the most obscure and convoluted installation procedure I’ve ever encountered. Wouldn’t it have been simpler to just put some zip files up on the site in the first place?

Advertisements
Categories: .NET Tags:
  1. Timotei
    April 4, 2009 at 7:46 am

    Thanks a lot for the fix. Very good 😉

  2. vai
    June 24, 2011 at 9:48 pm

    You’re a legend. Thanks for posting what I couldn’t be arsed figuring out on my own 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: