The "code" for doing this isn't that simple. I could not give you a solution based upon a one-sentence requirement. There are a number of things you need to consider. For example, how do you want the key to be generated (based off of computer information, user information, registration information, etc.)? Next, do you want to store this information centrally so the application can look to a server to verify itself or do you want to trust your application to verify its own key?
Here is a Stack Overflow question that has some great answers to this type of question:
http://stackoverflow.com/questions/453030/how-can-i-create-a-product-key-for-my-c-sharp-app[
^]
While I'm not necessarily saying you have to use a commercial application to do this, look through what this commercial application does and why:
http://www.eziriz.com/[
^]
Now look at what you were trying to do. Is what you were trying to do going to solve the issues that this application solves? Does it need to? Usually I find that people don't realize how big an issue this can really end up being. Other times, people just want something simple and they find that creating keys might not be a simple solution but that there are alternatives.