Skip to content

Instantly share code, notes, and snippets.

@duncansmart
duncansmart / DotNetZipOneFile.cs
Last active August 29, 2015 14:11
Creates a single source file version of DotNetZip
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace ZipReducedOneFile
{
class Program
{
static void Main()
@duncansmart
duncansmart / RouteUtils.cs
Last active August 29, 2015 14:08
GetRouteDataForPath: GetRouteData for a given path/URL (ASP.NET MVC)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
public static class RouteUtils
{
/// <summary>
/// e.g. <code>var routeData = RouteTable.Routes.GetRouteDataForPath("~/foo/bar");</code>
@duncansmart
duncansmart / ios-sms.sql
Created September 25, 2014 12:29
Query to extract iOS Messages from backup
-- Do a backup to iTunes and open "%APPDATA%\Apple Computer\MobileSync\Backup\*\3d0d7e5fb2ce288813306e4d4636395e047a3d28" as a SQLite database
SELECT chat.chat_identifier, message.is_from_me, datetime(message.date + 978307201, 'unixepoch') as date, message.text
FROM chat
JOIN chat_message_join on chat.ROWID = chat_message_join.chat_id
JOIN message on message.ROWID = chat_message_join.message_id
order by message.date
@duncansmart
duncansmart / gist:f28ae2940c7ef08aa6bb
Last active November 16, 2017 14:00
EF DbContext setup/teardown for tests
[TestFixture]
public class FooController_Tests
{
FooDbContext _dbContext = new FooDbContext(@"SERVER=(localdb)\v11.0; DATABASE=FooControllerTests_"+ Guid.NewGuid().ToString("n"));
[TestFixtureSetUp]
public void Setup()
{
_dbContext.Database.CreateIfNotExists();
}
using System;
using System.Speech.Synthesis; // Add System.Speech from Add Reference
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your name: ");
var name = Console.ReadLine();
@duncansmart
duncansmart / PdfSplitAndMerge.cs
Created February 3, 2014 13:51
Split PDFs into PDF per page (which can then be edited by InkScape) and merge
static void pdfSplitIntoPages()
{
// EO.Pdf.Runtime.AddLicense("...");
string sourceFile = @"C:\TEMP\Blah.pdf";
var pdf = new PdfDocument(sourceFile);
var pages = pdf.Split(Enumerable.Range(1, pdf.Pages.Count-1).ToArray());
var i = 1;
foreach (PdfDocument page in pages)
{
@duncansmart
duncansmart / select2-combo.js
Created October 17, 2013 16:14
Convert textbox to Select2 drop-down that allows new entries to be added (i.e. a combo box)
(function () {
var choices = [
"Foo",
"Bar",
"Baz"
];
$('#myTextBox')
.closest('.controls').append('<div class="help-block">Choose existing or type new one and press Enter</div>').end()
@duncansmart
duncansmart / EO-PDF-Forms.cs
Created October 8, 2013 22:46
Use EO.PDF to add form fieldst o a PDF
//File.WriteAllText(@"c:\temp\before.htm", html);
// Ensure all inputs have an ID
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionWriteEmptyNodes = true;
htmlDoc.LoadHtml(html);
var srcInputs = htmlDoc.DocumentNode.SelectNodes("//input");
foreach (var item in srcInputs)
{
if (item.Id.Trim().Length == 0)
@duncansmart
duncansmart / Custom Audit Events in ASP.NET.md
Last active December 22, 2015 06:09
Custom Audit Event

Custom Audit Class:

using System.Web.Management;
...
public class MyAuditEvent : WebAuditEvent
{
    public MyAuditEvent(string message, object eventSource, int eventCode)
        : base(message, eventSource,
               (eventCode < WebEventCodes.WebExtendedBase ? WebEventCodes.WebExtendedBase + eventCode : eventCode))

{

@duncansmart
duncansmart / CompileDataBinder.cs
Created August 1, 2013 11:10
A Reflection.Emit based version of DataBinder.Eval
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
class Program
{
static void Main(string[] args)
{
Person model = new Person