Skip to content

Instantly share code, notes, and snippets.

@maftieu
maftieu / gist:9a3d1532aa968e68f6a2
Last active August 29, 2015 14:11
C# - FormatFileSize
static string FormatFileSize(long value, string format = null)
{
var multipliers = new string[] { "o", "Kio", "Mio" };
var midx = 0;
while ((Math.Abs(value) >= 1024) && (midx < multipliers.Length))
{
value /= 1000;
++midx;
}
@maftieu
maftieu / gist:f5c8f18488fcf6f3112b
Last active August 29, 2015 14:11
C# - Changes the extension of a file
/// <summary>
/// Changes the file extension by moving it.
/// </summary>
/// <param name="filePath">Full path to the file for which change the extension.</param>
/// <param name="extension">The new file extension.</param>
/// <returns>The path to the file, with the new extension.</returns>
private string ChangeFileExtension(string filePath, string extension)
{
if (!extension.StartsWith("."))
extension = "." + extension;
@maftieu
maftieu / Extensions.cs
Last active February 5, 2016 13:46
C# - IEnumerable extensions
using System;
using System.Collections.Generic;
using System.Linq;
namespace Untunh.Helpers
{
/// <summary>
/// Provides a set of static methods for querying objects that implement System.Collections.Generic.IEnumerable<T>.
/// </summary>
public static class Extensions
@maftieu
maftieu / EnumExtensions.cs
Created January 15, 2015 11:23
C# - Extension methods for Enum
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Untunh.Helpers
{
/// <summary>
/// Provides a set of static methods for querying objects that represent an enumeration.
/// </summary>
@maftieu
maftieu / Benchmark.cs
Last active February 5, 2016 13:01
Quick benchmark method
using System;
class Benchmarker
{
const int NB_RUN = 5;
public static void Benchmark(Action testMethod, string testName = null, bool blankRun = false)
{
if (string.IsNullOrEmpty(testName))
testName = testMethod.Method.Name;
@maftieu
maftieu / vs.ps1
Last active August 29, 2015 14:26
Edit all C# files inside a project
# this requires NuGet to be installed, so it will bring the PowerShell command line
# and the EnvDTE object. See: https://msdn.microsoft.com/en-us/library/EnvDTE.aspx
function DoItOnProjectItem($item) {
$doc = $item;
if ($doc.Name -match "\.cs$") {
write-host $doc.Name;
# open and display document
$foo = $doc.Open("{7651A701-06E5-11D1-8EBD-00A0C90F26EA}"); # vsViewKindCode -> to display code window
@maftieu
maftieu / generate-pass.html
Created July 28, 2015 14:42 — forked from flesler/generate-pass.html
Bookmarklet to generate deterministic passwords based on a service name
<pre>
(function(length, prefix, suffix, map) {
/* Version 1.0 */
var p = location.hostname.split('.');
// Find service name
do { var pass = p.pop(); }
while (p.length && pass.length <= 3);
// First time assume domain
if (!this._custom_) {
this._custom_ = true;
@maftieu
maftieu / app.py
Last active August 29, 2015 14:26 — forked from panchr/app.py
Queued (I/O intensive) operations in Python
from queue_db import QueuedWriter
from pymongo import Connection
from flask import Flask
from json import dumps
def main():
'''This is just an example using Flask and MongoDB/pymongo.
Of course, you can use any web server and any database as long as you set up the methods appropriately'''
@maftieu
maftieu / shrink.sql
Created September 17, 2015 10:53
Shrink database
-- réduire les LOGS
BACKUP LOG [Concours] TO DISK = N'E:\Backup\Concours-logs.bak'
USE [Concours]
ALTER DATABASE [Concours] SET RECOVERY SIMPLE WITH NO_WAIT
DBCC SHRINKFILE(Concours_Log, 1)
ALTER DATABASE [Concours] SET RECOVERY FULL WITH NO_WAIT
-- réduire le fichier de DATA
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/[platform] folder.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];