Skip to content

Instantly share code, notes, and snippets.

View aleksandrzak-rafal's full-sized avatar

devstroops aleksandrzak-rafal

View GitHub Profile
@aleksandrzak-rafal
aleksandrzak-rafal / ElevatedCommandPromptForScriptExecution.ps1
Created February 21, 2017 10:29
Code to get elevated command prompt for script execution
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
@aleksandrzak-rafal
aleksandrzak-rafal / data_reader_to_class
Created March 30, 2016 11:24
Clever way to map data from DataReader to class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public static Employee Create(IDataRecord record)
{
return new Employee
{
Id = record["id"],
@aleksandrzak-rafal
aleksandrzak-rafal / odbc_safe_get
Created March 30, 2016 11:22
Extension method for OdbcDataReader to get rid of null checking when calling Get functions.
public static T SafeGet<T>(this OdbcDataReader reader, int colIndex)
{
if (!reader.IsDBNull(colIndex))
{
return (T)reader.GetValue(colIndex);
}
else
{
return default(T);
}

This is my list of code snippets useful when writing (geo)graphical applications in C#. Of course there are many powerful .NET libraries which handle these problems. But sometimes it is easier to just include some lines of code.

I've written the code to run directly in the browser using JSIL. You can extract the essential lines in your project. It should work for .NET, Mono, Silverlight, WinRT and Windows Phone. I've included the base types needed (Point, Rect) into the snippet. You can replace them with the appropriate type of the framework you are using.

For example: If you use the line simplification for WPF lines, you can take the System.Windows.Point. If you use it for WCF Spatial Library, you can take the [System.Spatial.GeometryPoint](http://msdn.microsoft.co