Skip to content

Instantly share code, notes, and snippets.

@fresky
fresky / GetDriveInfo.cs
Last active December 14, 2015 03:39
Get Drive Information in C#
foreach (var s in DriveInfo.GetDrives())
{
if (s.IsReady)
{
Console.WriteLine(s.Name);
Console.WriteLine(s.TotalSize);
Console.WriteLine(s.AvailableFreeSpace);
}
@fresky
fresky / KillThread.cs
Last active July 10, 2019 08:31
Kill thread in C#
class Program
{
static int threadID=0;
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
@fresky
fresky / AssemblyResolve.cs
Created December 22, 2012 05:45
AssemblyResolve example
public class MyClass
{
public MyClass()
{
Console.WriteLine("MyClass is created!");
}
}
public class Program
@fresky
fresky / DelegateFuction.cs
Created December 10, 2012 06:36
Watch out the ToString() and ToString in the delegate.
public class Program
{
public static int StaticInt = 0;
static Func<string> delegate1 = new Func<string>(StaticInt.ToString);
static Func<string> delegate2 = new Func<string>(() => StaticInt.ToString());
static void Main(string[] args)
{
@fresky
fresky / NoDerive.cpp
Created December 4, 2012 15:56
How to make a class non derivable in C++
class NoDerive {
NoDerive(){};
public:
static NoDerive GetNoDerive(){ return NoDerive();};
~NoDerive(){};
};
@fresky
fresky / ArrayTrap.cpp
Created December 4, 2012 13:39
Example code to show can NOT use Base* for the array of Sub
#include <iostream>
#include <assert.h>
using namespace std;
class B;
class A
{
public:
A();
~A();
@fresky
fresky / IsFatalException.cs
Created October 15, 2012 05:25
Check exception is fatal or not
public static bool IsFatal(this Exception exception)
{
while (exception != null)
{
if (exception as OutOfMemoryException != null && exception as InsufficientMemoryException == null || exception as ThreadAbortException != null ||
exception as AccessViolationException != null || exception as SEHException != null || exception as StackOverflowException != null)
{
return true;
}
else
@fresky
fresky / ProcessDetector.cs
Created October 9, 2012 07:25
Check one process is 32bit or 64bit
static class ProcessDetector
{
public static bool IsWin64(Process process)
{
if (Environment.Is64BitOperatingSystem)
{
IntPtr processHandle;
bool retVal;
try
@fresky
fresky / XMLSerialize.cs
Created September 28, 2012 05:20
XML Serialize without namespace and xml declaration
ObjectToSerialize objectToSerialize = new ObjectToSerialize();
StringBuilder sb = new StringBuilder();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlWriter.Create(new StringWriter(sb), settings);
xmlSerializer.Serialize(xmlWriter, objectToSerialize, ns);
Console.WriteLine(sb.ToString());
@fresky
fresky / CopyDirectory.cs
Created September 17, 2012 08:17
Copy directory in C#
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
//Copy all the files
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));