Tagged: snippet RSS

  • Steve Lydford 2:38 pm on October 22, 2010 Permalink | Reply
    Tags: , , , snippet   

    Unique Key Generator 

    This morning I needed to generate a unique key for an application I am working on. The key needed to be unique within the application, but I didn’t need it to be globally unique and a GUID is just too long.

    I have played in the past with various combinations of DateTime, SessionID and other application specific values to cobble together an application wide unique key but really wanted something much neater, like the unique values created by sites such as TinyUrl.com

    For example: http://tinyurl.com/39gg3es

    So, after a little research and playing around, I came up with this:

    
    static string GenerateKey(int keyLength) {
        char[] maskChars = "1234567890AaBcCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".ToCharArray(); 
    
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        byte[] cryptoBytes = new byte[keyLength];
        crypto.GetNonZeroBytes(cryptoBytes); 
    
        StringBuilder key = new StringBuilder(keyLength);
            foreach(byte b in cryptoBytes)
        key.Append(maskChars[b % 62]);
        return key.ToString();
    }
    

    You will need to import

    System.Security.Cryptography;

    and can create a unique key by using the following code:

    
    string uniqueKey = GenerateKey(6);
    

    specifying the length of the key you require (in this case six). This will generate a key such as: D7D2iU

    Please feel free to use this code as you wish. If you do use it I would love to know where and, of course, any comments or suggestions would be appreciated.

    Digg This
    Reddit This
    Stumble Now!
    Buzz This
    Vote on DZone
    Share on Facebook
    Bookmark this on Delicious
    Kick It on DotNetKicks.com
    Shout it
    Share on LinkedIn
    Bookmark this on Technorati
    Post on Twitter
    Google Buzz (aka. Google Reader)
     
    • Softwarepadawan 6:33 pm on February 1, 2011 Permalink

      Although your key generator algorithm is _very_ good it does not produce unique keys. You need to change the title. BUT! It’s still a nice way of generating keys. Keep up the good work!

    • Filip Zawada 7:34 pm on February 1, 2011 Permalink

      Nice code but if you need something shorter than 32 then why not just:
      static string SimpleKey(int keyLength)
      {
      if (keyLength > 32)
      {
      //start yelling
      }
      return Guid.NewGuid().ToString().Replace(“-”, “”).Substring(0, keyLength);
      }

      Faster and easier, unless I’m missing something from your post.

  • Steve Lydford 11:21 am on August 4, 2010 Permalink | Reply
    Tags: , , , snippet   

    “Works On My Machine” Code Snippet 

    Here’s a ‘useful’ snippet I just wrote for C# ASP.Net in Visual Studio. Type “womm”, hit tab twice and you’ll get this…

    try
    {
    
    }
    catch (Exception)
    {
        Response.Write("That's weird!<br />");
        Response.Write("It works on my machine.");
    }
    

    To use it either download the snippet or copy the code below and save it as womm.snippet, and drop it into:

    My Documents\Visual Studio 2010 (or 2008)\Code Snippets\Visual C#\My Code Snippets

    or add it to Visual Studio using the Code Snippets Manager found in the Tools menu.

    Enjoy! ;)

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    	<CodeSnippet Format="1.0.0">
    		<Header>
    			<Title>womm</Title>
    			<Shortcut>womm</Shortcut>
    			<Description>Code snippet for WOMM try catch</Description>
    			<Author>Steve Lydford</Author>
    			<SnippetTypes>
    				<SnippetType>Expansion</SnippetType>
    				<SnippetType>SurroundsWith</SnippetType>
    			</SnippetTypes>
    		</Header>
    		<Snippet>
    			<Declarations>
    				<Literal>
    					<ID>expression</ID>
    					<ToolTip>Exception type</ToolTip>
    					<Function>SimpleTypeName(global::System.Exception)</Function>
    				</Literal>
    			</Declarations>
    			<Code Language="csharp"><![CDATA[try
    	{
    		$selected$
    	}
    	catch ($expression$)
    	{
    		$end$
    		Response.Write("That's weird!<br />");
    		Response.Write("It works on my machine.");
    	}]]>
    			</Code>
    		</Snippet>
    	</CodeSnippet>
    </CodeSnippets>
    
    Digg This
    Reddit This
    Stumble Now!
    Buzz This
    Vote on DZone
    Share on Facebook
    Bookmark this on Delicious
    Kick It on DotNetKicks.com
    Shout it
    Share on LinkedIn
    Bookmark this on Technorati
    Post on Twitter
    Google Buzz (aka. Google Reader)
     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel