Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / IsEmail.cs
Created June 4, 2012 13:30
IsEmail extension method
public static bool IsValidEmail(string email)
{
string pattern = @"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$";
Regex regex = new Regex(pattern);
return regex.IsMatch(email);
}
@Dynyx
Dynyx / SessionState.cs
Created June 4, 2012 13:30
Session state dictionary
public class SessionDictionary : IDictionary<string,object>, IDictionary
{
readonly HttpSessionState sessionState;
readonly CollectionAdapter<string> keysAdapter;
readonly CollectionAdapter<object> valuesAdapter;
public SessionDictionary(HttpSessionState sessionState)
{
this.sessionState = sessionState;
keysAdapter = new CollectionAdapter<string>(sessionState.Keys);
@Dynyx
Dynyx / Thumbnailer.cs
Created June 4, 2012 13:29
Thumbnail generator
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
namespace Thumbnailer
{
public static class Thumbnail
{
@Dynyx
Dynyx / Sanitize.cs
Created June 4, 2012 13:28
User input sanitization
// Uses Tidy.Net
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Security.Application;
using TidyNet;
@Dynyx
Dynyx / Site.css
Created June 4, 2012 13:28
Default site.css
*{font-family:"lucida grande",tahoma,verdana,arial,sans-serif}
.aligncenter{text-align:center}
body{padding:0;margin:0;color:#333;line-height:1.3}
body,input.textbox,select,ul.nav li a{font-size:14px}
.centered{margin:0 auto}
.floatleft{float:left}
.floatright{float:right}
h1,h2,h3,h4{margin:0;padding:0}
input.textbox{padding:5px}
select{height:31px;padding:5px}
@Dynyx
Dynyx / Singleton.cs
Created June 4, 2012 13:27
Thread-safe singleton pattern
// In software engineering, the singleton pattern is a design pattern used to implement the mathematical
// concept of a singleton, by restricting the instantiation of a class to one object. This is useful when
// exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized
// to systems that operate more efficiently when only one object exists, or that restrict the instantiation
// to a certain number of objects.
public sealed class MySingleton
{
private static MySingleton instance;
private static readonly Object sync = new object();
@Dynyx
Dynyx / XMLSerialization.cs
Created June 4, 2012 13:27
XML serialization functions
#region " XML Serialization "
/// <summary>
/// serialize an object to XML
/// </summary>
/// <typeparam name="T">the type of the input object</typeparam>
/// <param name="objectToSerialize">the object to serialize</param>
/// <returns></returns>
public static string XmlSerialize<T>(T objectToSerialize)
{
@Dynyx
Dynyx / FileSystemWatcher.cs
Created June 4, 2012 13:23
FileSystemWatcher example
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
Run();
@Dynyx
Dynyx / ControllerActionMVC.cs
Created June 4, 2012 13:21
Get controller and action name in MVC
var controllerName = ViewContext.RouteData.Values["controller"].ToString().ToLower();
var actionName = ViewContext.RouteData.Values["action"].ToString().ToLower();
@Dynyx
Dynyx / LINQ2XML.cs
Created June 4, 2012 13:20
LINQ2XML example
<?xml version="1.0" encoding="utf-8" ?>
<Patients>
<Patient EMail="[email protected]">
<FirstName>LeBron</FirstName>
<LastName>James</LastName>
</Patient>
<Patient EMail="[email protected]">
<FirstName>Kobe</FirstName>
<LastName>Bryant</LastName>
</Patient>