Skip to content

Instantly share code, notes, and snippets.

@fresky
fresky / ProcessStartTime.cs
Created July 6, 2012 04:48
Get the start time of C# process
System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
DateTime startedTime = current.StartTime;
@fresky
fresky / EnumLoop.cs
Created July 17, 2012 00:56
Loop Enum
enum ProgrammingLanguage
{
Language_First = 0,
CPP = 1,
CSharp = 2,
Java =3,
Language_Last = 4,
}
class Program
{
@fresky
fresky / DisableTreeNode.cs
Created July 17, 2012 03:17
How to disable C# tree node
private void Tree_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = IsTreeNodeSelectable(e.Node);
}
@fresky
fresky / Cast.cs
Created July 17, 2012 07:02
C# cast example
class C<T> {}
internal class D
{
public static C<U> M<U>(C<bool> c)
{
// Fail compiling:
// return (C<U>)c;
// Pass compiling, exception in runtime:
// return X.Cast<C<U>>(c);
@fresky
fresky / StartProcess.cs
Created July 20, 2012 09:22
Check whether a process is started or not, if not, then start it in background
Regex regex = new Regex(@"app");
bool found = false;
foreach (var process in Process.GetProcesses())
{
if (regex.Match(process.ProcessName).Success)
{
found = true;
break;
}
@fresky
fresky / StringComparison.cs
Created July 23, 2012 09:18
c# StringComparison example
Console.WriteLine("CurrentCulture: " + StringComparison.CurrentCulture);
Console.WriteLine("InvariantCulture: " + StringComparison.InvariantCulture);
Console.WriteLine("Ordinal: " + StringComparison.Ordinal);
Console.WriteLine();
const string softhyphen = "\xAD";
const string hyphen = "\x2D";
const string softHyphenPlusHyphen = "\xAD\x2D";
const string hyphenPlusSoftHyphen = "\x2D\xAD";
@fresky
fresky / AppDir.cs
Created July 24, 2012 07:55
Get the directory of the application
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
@fresky
fresky / GetAssemblyTypes.cs
Created July 24, 2012 08:54
Get Assembly Types
public static IEnumerable<Type> GetAssemblyTypes(string filePath)
{
Assembly assembly =
Assembly.LoadFile(filePath);
IEnumerable<Type> types;
try
{
types = assembly.GetTypes();
}
@fresky
fresky / Extension.cs
Created July 25, 2012 09:08
Extension method for c#
public static class Extention
{
public static bool IsAbstrct(this Type type)
{
return type.IsAbstract;
}
public static bool IsEnumerableType(this Type t)
{
return typeof(IEnumerable<string>).IsAssignableFrom(t);
@fresky
fresky / Screen.cs
Created August 14, 2012 02:03
Get screen resolution
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeigth