Skip to content

Instantly share code, notes, and snippets.

@duncansmart
duncansmart / TransferToRouteResult.cs
Created June 27, 2013 12:20
MVC way to do Server.Transfer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Foo.Bar
{
// Adpated from http://stackoverflow.com/questions/799511/how-to-simulate-server-transfer-in-asp-net-mvc
@duncansmart
duncansmart / httpget.js
Created June 20, 2013 09:42
Download a file with Windows Script Host
// httpget.js: download a file (Windows Script Host)
// usage: cscript httpget.js <url> <file>
(function() {
if (WScript.Arguments.Length != 2) {
WScript.Echo("Usage: httpget.js <url> <file>")
WScript.Quit(1)
}
var url = WScript.Arguments(0)
@duncansmart
duncansmart / progressive-ace.htm
Created March 28, 2013 23:29
Integrating ACE Editor in a progressive way
<textarea name="my-xml-editor" data-editor="xml" rows="15"></textarea>
...
<textarea name="my-markdown-editor" data-editor="markdown" rows="15"></textarea>
...
<script src="//d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js"></script>
<script>
// Hook up ACE editor to all textareas with data-editor attribute
$(function () {
@duncansmart
duncansmart / BackupEfsCerts.cs
Created March 28, 2013 23:22
Backup current user's EFS Certificates (including private keys)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
class CertDoodle
{

Bookmarklet to switch between HackerNews and HackerWeb

This bookmarklet toggles between HackerNews pages and Cheeuan's HackerWeb interface. If you're not on either then it acts a a bookmark to HackerWeb.

The code

If you edit this, use something like bookmarkleter to convert it into a bookmarklet.

if ( /https?:\/\/news\.ycombinator\.com\/item\?id=(\d+)/.test(location) ) {

location = 'http://cheeaun.github.io/hackerweb/#/item/' + RegExp.$1;

@duncansmart
duncansmart / CreateSystemRestorePoint.js
Created December 6, 2012 08:06
Create System Restore Point (Windows)
// call with %systemroot%\System32\cscript.exe, schedule with Task Scheduler
var systemRestore = GetObject("winmgmts:\\\\.\\root\\Default:SystemRestore")
var result = systemRestore.CreateRestorePoint("Scheduled restore point", 0, 100)
WScript.Echo("result " + result)
@duncansmart
duncansmart / gist:4014051
Created November 4, 2012 22:28
Ensure SQL Service Broker is enabled
static void ensureServiceBrokerEnabled()
{
var sql = @"if (SELECT is_broker_enabled FROM sys.databases WHERE name = DB_NAME()) = 0
begin
declare @sql nvarchar(max) = 'ALTER DATABASE ' + QUOTENAME(DB_NAME()) + ' SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE'
exec(@sql)
end";
using (var db = DependencyResolver.Current.GetService<IDbConnection>())
{
db.Open();
@duncansmart
duncansmart / get table schema.sql
Created November 2, 2012 16:45
TSQL - Get table columns, types, primary keys, etc
select
SchemaName = schema_name(t.schema_id)
, TableName = t.name
, ColumnName = c.name
, IsPrimaryKey = (select count(*) from
sys.indexes as i join sys.index_columns as ic on i.OBJECT_ID = ic.OBJECT_ID and i.index_id = ic.index_id and ic.column_id = c.column_id
where i.is_primary_key = 1 and i.object_id = t.object_id)
, ColumnType = TYPE_NAME(c.system_type_id)
, MaxLength = c.max_length
from sys.tables t join sys.columns c on t.object_id = c.object_id
@duncansmart
duncansmart / gist:3982912
Created October 30, 2012 20:45
Guid compression
static string compressGuid(Guid guid)
{
// "MspzuC2oLkKjoUEgZrbJFg=="
return Convert.ToBase64String(guid.ToByteArray())
.Substring(0, 22)
.Replace('+', '-')
.Replace('/', '_');
}
@duncansmart
duncansmart / MailUtil.cs
Created October 10, 2012 22:25
GetMXRecords in C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class MailUtil
{
#region DLL Imports