Skip to content

Instantly share code, notes, and snippets.

@benerdin
benerdin / SomeController.cs
Last active August 29, 2015 14:18
ASP.NET Web API - Download File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
@benerdin
benerdin / TimeZoneConverter.cs
Last active August 29, 2015 14:04
TimeZoneConverter
using System;
using System.Linq;
namespace TimeZones
{
/// <summary>
/// Responsible for converting between different time zones.
/// </summary>
public class TimeZoneConverter
{
@benerdin
benerdin / prevent-pasting-into-text-input.js
Created July 17, 2014 14:06
How to prevent pasting into a text field using jQuery.
$(document).ready(function() {
$('#elementId')
.keypress(function(e) {
// Prevent users from being able to type into the field.
e.preventDefault();
})
.keydown(function (e) {
// Prevent users from being able to type into the field.
e.preventDefault();
})
@benerdin
benerdin / RestSharp.cs
Created June 10, 2014 18:14
RestSharp and ServiceStack nUnit tests for POSTing form data
[TestFixture]
class RestSharpIntegration
{
[Test]
public void EnsureFormIsPosted()
{
// Arrange
var request = CreateRequest();
// Act
@benerdin
benerdin / manually-kill-a-service.bat
Created February 27, 2014 16:25
Manually kill a process for a Windows Service that won't stop.
sc queryex wuauserv
SERVICE_NAME: wuauserv
DISPLAY_NAME: Windows Update
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
@benerdin
benerdin / delete-win32-service.bat
Last active August 29, 2015 13:56
Delete Windows Service from command-line
sc delete <name-of-service>
@benerdin
benerdin / CollateEnumerator.cs
Last active August 29, 2015 13:56
An enumerator that collates an IGrouping data structure.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Brian.Federici
{
/// <summary>
/// An object that can iterate over an IGrouping collection.
/// </summary>
/// <typeparam name="T">Value type.</typeparam>
@benerdin
benerdin / CompressionExtensions.cs
Created January 14, 2014 22:27
Extension methods for compressing and decompressing a string using GZipStream.
/// <summary>
/// Encapsulates extension methods related to compression.
/// </summary>
public static class CompressionExtensions
{
/// <summary>
/// Compresses <paramref name="uncompressedString"/> and returns the result.
/// </summary>
/// <param name="uncompressedString">An uncompressed string.</param>
/// <returns>A base-64 string.</returns>
@benerdin
benerdin / DownloadAndResizeStream.cs
Last active January 1, 2016 18:59
Download and resize an image to a Bitmap using the ImageResizer nuget package. The resulting Bitmap is copied to a MemoryStream and returned.
/*
Make sure to wrap a using() around any calls to DownloadAndResizeStream:
using (var stream = DownloadAndResizeStream(uri, 100, 100, true)) {
// Do what you need to with "stream" here
}
*/
private Stream DownloadAndResizeStream(Uri sourceImageUrl, int width, int height, bool crop)
{
@benerdin
benerdin / nant-foreach-xmlpoke.xml
Last active December 29, 2015 00:59
Example of how to use nAnt's <foreach> and <xmlpoke> tasks.
<!--
Given the following files in C:\
* app.1.config
* app.2.config
* app.3.config
* file.txt
This task will perform 3 iterations and set ${filename} to the following:
* C:\app.1.config
* C:\app.2.config