Skip to content

Instantly share code, notes, and snippets.

View csharpforevermore's full-sized avatar
🏠
Working from home

Randle csharpforevermore

🏠
Working from home
View GitHub Profile
@csharpforevermore
csharpforevermore / DirByCreationDate.pl
Created October 27, 2013 21:09
List the contents of a directory - by creation date. I use this script for when downloading large numbers of files and need to rename them later, although there are many other uses of course.
use 5.12.0;
use warnings;
use File::stat;
my $dir_name = shift;
if ( not defined $dir_name ) {
die qq(Usage: $0 <directory>);
}
@csharpforevermore
csharpforevermore / App_Code-GlobalHelpers.cshtml
Created October 28, 2013 15:55
Umbraco 6 - Razor - Global helper wrapper method for rending a hyperlink
@helper RenderLink(string href, string title, string target, string text)
{
<a href="@(href)" title="@(title)" target="@(target)">@(text)</a>
}
@csharpforevermore
csharpforevermore / GetAbsoluteUrlFromRelative.cs
Created October 31, 2013 15:31
C# ASP.NET - get the absolute URL from a relative one
string relativeUrl = "/home";
string returnValue = string.Format("{0}://{1}{2}{3}", current.Scheme, current.Host,
current.Port == 80 ? "" : ":" + current.Port, relativeUrl)
@csharpforevermore
csharpforevermore / IEnumToCsv.cs
Created November 7, 2013 10:37
C# - get a CSV of a property of IEnumerable
public class Car{
public int Id {get; set;}
public string Make {get; set;}
public string Model {get; set;}
}
private string Get
var enumerableCollection = new List<Car>();
@csharpforevermore
csharpforevermore / ReplaceUnprintableCharacters.cs
Created November 11, 2013 18:21
Replace unprintable characters in a string
private string GetRidOfUnprintablesAndUnicode(string inpString)
{
return inpString.Where(ch => ((int) (byte) ch) >= 32 & ((int) (byte) ch) <= 128).Aggregate(String.Empty, (current, ch) => current + ch);
}
@csharpforevermore
csharpforevermore / GetXmlValue.cs
Created November 11, 2013 18:22
Get value from a string version of an XML node (e.g. Get text "Value" from this <node id="test>Value</node> )
private string GetXmlValue(string responseUrl)
{
return responseUrl.Split('>')[1].Split('<')[0];
}
@csharpforevermore
csharpforevermore / Log.cs
Created November 12, 2013 11:21
Entity Framework, code-first example
using System;
using System.Data.Entity;
namespace Website.Models.Logging
{
public class Log
{
public int LogId { get; set; }
public DateTime TimeStarted { get; set; }
public int Total { get; set; }
@csharpforevermore
csharpforevermore / GetXmlElementFromUrl.cs
Created November 14, 2013 14:25
Read the XML document element (elementName) at a given URL (url). Allows for encoding problems.
public string GetXmlElementFromUrl(string url, string elementName)
{
byte[] data;
using (var webClient = new WebClient())
data = webClient.DownloadData(url);
string str = Encoding.GetEncoding("Windows-1252").GetString(data);
XDocument xmlDoc = XDocument.Parse(str);
var donation = new Donation();
@csharpforevermore
csharpforevermore / PostToWebservice.cs
Created November 14, 2013 14:29
Post an XML document to the web service at the URL
public byte[] Register(string url, string emailAddress, string firstName, string lastName)
{
var swr = new StringWriter();
var xws = new XmlWriterSettings {OmitXmlDeclaration = true};
using (XmlWriter xwr = XmlWriter.Create(swr, xws))
{
xwr.WriteStartElement("register");
xwr.WriteAttributeString("email", emailAddress);
xwr.WriteAttributeString("firstname", firstName);
@csharpforevermore
csharpforevermore / AllFilesRenameAndNumber.pl
Last active December 28, 2015 08:59
Rename mp4 files to file1.mp4, file2.mp4, file3.mp4, etc
use 5.12.0;
use warnings;
use File::Spec;
use File::stat;
my $dirname = shift;
my $ext = 'mp4';
my $basename = "file";
my $count = 1;
my $new;