posted on Wednesday, October 20, 2004 2:51 PM by stombeur

Creating GUIDs in VS 2005 B1

In VS.NET 2003 there is an external tool called 'GuidGen' to generate GUIDs. I can't seem to find this in VS 2005 so I made my own:

class Program
{
    public const string ARG_BYTES = "/b";
    public const string ARG_CSHARP = "/cs";
    public const string ARG_HELP = "/?";

    static void Main(string[] args)
    {
        StringBuilder builder = new StringBuilder();

        if (args.Length > 0)
        {
            switch (args[0])
            {
                case ARG_BYTES:
                    foreach (byte b in Guid.NewGuid().ToByteArray())
                    {
                        builder.Append(b.ToString("X2"));
                        builder.Append(" ");
                    }
                    break;
                case ARG_CSHARP:
                    builder.Append("new byte [] { ");
                    foreach (byte b in Guid.NewGuid().ToByteArray())
                    {
                        builder.Append("0x");
                        builder.Append(b.ToString("X2"));
                        builder.Append(", ");
                    }
                    builder.Remove(builder.Length - 2, 2);
                    builder.Append(" }");
                    break;
                case ARG_HELP:
                default:
                    builder.Append(string.Format("Usage: no arguments for a hexString representation, {0} for a byteArray representation, {1} for a C# byte array, {2} for this help message", ARG_BYTES, ARG_CSHARP, ARG_HELP));
                    break;
            }
           
        }
        else
        {
            builder.Append(Guid.NewGuid().ToString());
        }

        Console.Write(builder.ToString());
    }
}

 

At the command line type:

"GuidGen" for output like "ab04c0a1-ebb7-4996-914f-2a16ca9212f4"

"GuidGen /b" for output like: "31 34 37 18 5F 5B 2A 45 BC 8C 32 06 37 BF E9 B6"

"GuidGen /cs" for output like "new byte [] { 0x38, 0x39, 0xC0, 0xBD, 0xCE, 0xD4, 0xEC, 0x41, 0x8C, 0x90, 0x7C, 0x96, 0x37, 0x35, 0x83, 0x1C }"

Comments