Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / UnhandledException.cs
Created June 4, 2012 13:12
Unhandled exception handling
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
@Dynyx
Dynyx / Twitter.cs
Created June 4, 2012 13:11
Tweet using Twitter API
// Requires the use of the Twitterer Nuget package
public static RequestResult Tweet(string message)
{
var authTokens = new OAuthTokens
{
AccessToken = ConfigurationManager.AppSettings["AccessToken"],
AccessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"],
ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]
@Dynyx
Dynyx / bitly.cs
Created June 4, 2012 13:11
Bit.ly URL shortening
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
namespace MyApp
{
public class Bitly
{
@Dynyx
Dynyx / CustomValidationContext.cs
Created June 4, 2012 13:10
Custom validation attribute with current context
public Func<HttpContextBase> ContextProvider = () => new HttpContextWrapper(HttpContext.Current);
public override bool IsValid(object value)
{
if (value == null) return false;
var identityName = ContextProvider().User.Identity.Name;
// ...
}
@Dynyx
Dynyx / LocalDomains.cs
Created June 4, 2012 13:09
Find local domains
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry dom in root.Children) {
foreach (DirectoryEntry entry in dom.Children) {
if (entry.Name != "Schema") {
Console.WriteLine(entry.Name);
}
}
}
@Dynyx
Dynyx / DivViewScroll.js
Created June 4, 2012 13:09
Keep a DIV in view as page scrolls
$().ready(function() {
var $scrollingDiv = $("#scrollingDiv");
$(window).scroll(function(){
$scrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow");
});
});
@Dynyx
Dynyx / CustomValidation.cs
Created June 4, 2012 13:08
Custom data validation using data annotations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
[MetadataType(typeof (FooBarMetaData))]
public class FooBar
{
// Insert FooBar related properties, methods, etc. here
@Dynyx
Dynyx / Age.cs
Created June 4, 2012 13:07
Calulate age from a given date
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;
@Dynyx
Dynyx / IDisposable.cs
Created June 4, 2012 13:06
IDisposable class example
public class ClassName : IDisposable
{
#region Variable Declarations
private bool _disposed = false;
#endregion
@Dynyx
Dynyx / EnumEnumerator.cs
Created June 4, 2012 13:06
Enum enumerator
Array lstProvider = System.Enum.GetValues( typeof(CommonShared.EnumProvider));
foreach (CommonShared.EnumProvider enProvider in lstProvider)
{
/// Do something
}