Skip to content

Instantly share code, notes, and snippets.

@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:19
Writing/Reading Registry using C#
using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey
("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:18
Complement Int32
private static byte GetPositionHigherBit(int value)
{
for (byte kk = 31; kk > 0; kk--)
if ((value & (int)(Math.Pow(2, kk))) == (int)(Math.Pow(2, kk))) return kk;
return 0;
}
private static int InvertBits(int value)
{
var posbit = GetPositionHigherBit(value);
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:17
Create Xml File
using System;
using System.Xml;
public class GenerateXml {
private static void Main() {
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("products");
@gscattolin
gscattolin / Read XML using Linq
Created September 10, 2013 18:15
Read XML using Linq
using System.Linq;
using Pdf_Rendering;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
XNamespace namespacexml= "http://www.heidelberg.com";
if (Path.GetExtension(fn).ToLower() != ".xml") { Console.WriteLine("Input file not a pdf file"); return false; }
if (!File.Exists(fn)) { Console.WriteLine("Input file does not exist"); return false; }
XDocument doc = XDocument.Load(fn);
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:14
Ip Validate
/// <summary>
/// method to validate an IP address
/// using regular expressions. The pattern
/// being used will validate an ip address
/// with the range of 1.0.0.0 to 255.255.255.255
/// </summary>
/// <param name="addr">Address to validate</param>
/// <returns></returns>
public bool IsValidIP(string addr)
{
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:08
String Format
String Format for Double
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:08
Enum Enumerator
//Fill a combobox with an enumerator
TypeofItems_cb.Items.Clear();
foreach (var it in Enum.GetValues(typeof(Recipe.ProductType)))
{
TypeofItems_cb.Items.Add(it.ToString());
}
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:07
Dispose Good Practice
Type That Owns Disposable Fields Implements IDisposable: Good Practice
public class OwnsDisposableFields : IDisposable
{
private DisposableResource disposableResourceOne;
private DisposableResource disposableResourceTwo;
// Other fields continue to be declared here.
private bool disposed;
~OwnsDisposableFields()
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:05
byte array to get object
/// <summary>
/// Function to get object from byte array
/// </summary>
/// <param name="_ByteArray">byte array to get object</param>
/// <returns>object</returns>
public object ByteArrayToObject(byte[] _ByteArray)
{
try
{
// convert byte array to memory stream
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:04
YAXLib: Yet Another XML Serialization Library for the .NET Framework
http://yaxlib.codeplex.com/
YAXLib: Yet Another XML Serialization Library for the .NET Framework