Skip to content

Instantly share code, notes, and snippets.

@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 / 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: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:20
Delete files in a folder/folder
private void ClearFolder(string FolderName)
{
DirectoryInfo dir = new DirectoryInfo(FolderName);
foreach (FileInfo fi in dir.GetFiles())
{
fi.IsReadOnly = false;
fi.Delete();
}
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:21
Get Assembly path
/// <summary>
/// Returns the path (with ending backslash) of current executing assembly
/// </summary>
/// <returns>Path of executing assembly</returns>
public static string AssemblyPath () {
return Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) +
Path.DirectorySeparatorChar;
}
/// <summary>
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:24
Deep copy, clone, ICloneable
// Shows how to return a deep copy clone of an object.
class CloneTest : ICloneable
{
public int SomeProperty { get; set; }
public object Clone()
{
CloneTest clone = new CloneTest();
clone.SomeProperty = this.SomeProperty;
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:25
Fluent NHibernate Get Info on Tables
var metadata = sf.GetAllClassMetadata().ToList();
IDictionary<string,IClassMetadata> data = sf.GetAllClassMetadata();
//Dictionary<string, SingleTableEntityPersister> lstTable = sf.GetAllClassMetadata().ToDictionary(x => x.Key, x=> x.Value);
foreach (var table in metadata)
{
Console.WriteLine("List {0}", table.ToString());
}
foreach(KeyValuePair<string,IClassMetadata> entry in data)
{
Console.WriteLine("Value {0} {1}",entry.Key,entry.Value.EntityName);
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:27
Create DB Nhibernate
const string Connstring = "server=SWIGVA01-SRV341;database=LeafPoint_Global;" +
"User Id=user;Password=pwd;";
configuration= Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008
.ConnectionString(Connstring).ShowSql())
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<FunctionMap>()
//.Conventions.Add(new PrimaryKeyNameConvention())
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:27
Create Session factory in Fluent NHibernate
private static ISessionFactory CreateSessionFactory()
{
//const string Connstring = "server=SWIGVA01-SRV341;database=LeafPoint_Global;" +
// "User Id=lp_global;Password=lp_gl0b4l;";
const string Connstring = @"server=.\SQLEXPRESS;database=SimpleDB;" +
"Integrated Security=SSPI;";
return Fluently.Configure()
.Database(MsSqlConfiguration
@gscattolin
gscattolin / new_gist_file
Created September 10, 2013 18:28
Access to SQL and add admin rights
shut down SQL Server from services
open cmd window (as admin) and run single-user mode as local admin with this command
c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn\sqlservr.exe" -m -s SQLEXPRESS
open another cmd window (as admin)
open sqlcmd