Prevent Users from Creating Folder in SharePoint Library

Suppose let's presume that if you want users  to prevent from creating a folder in SharePoint Document library, you can create a event receiver in SharePoint server side object model.
 
Here is the sample code snippet.
  
if (properties.AfterProperties["ContentType"].ToString() == "Folder")
     {
          properties.ErrorMessage = "Sorry!! You can't create any directory into the list";
                    properties.Cancel = true;
     }