<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Mstampar's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 23:57:53 GMT</pubDate>
    <description>DZone Snippets: Mstampar's Code Snippets</description>
    <item>
      <title>Compress/decompress byte array</title>
      <link>http://snippets.dzone.com/posts/show/4129</link>
      <description>&lt;code&gt;&lt;br /&gt; using System;&lt;br /&gt; using System.Collections.Generic;&lt;br /&gt; using System.IO.Compression;&lt;br /&gt; using System.IO;&lt;br /&gt; using System.Collections;&lt;br /&gt;&lt;br /&gt;namespace Utilities&lt;br /&gt;{&lt;br /&gt; class Compression&lt;br /&gt; {&lt;br /&gt;        public static byte[] Compress(byte[] data)&lt;br /&gt;        {&lt;br /&gt;            MemoryStream ms = new MemoryStream();&lt;br /&gt;            DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);&lt;br /&gt;            ds.Write(data, 0, data.Length);&lt;br /&gt;            ds.Flush();&lt;br /&gt;            ds.Close();&lt;br /&gt;            return ms.ToArray();&lt;br /&gt;        }&lt;br /&gt;        public static byte[] Decompress(byte[] data)&lt;br /&gt;        {&lt;br /&gt;            const int BUFFER_SIZE = 256;&lt;br /&gt;            byte[] tempArray = new byte[BUFFER_SIZE];&lt;br /&gt;            List&lt;byte[]&gt; tempList = new List&lt;byte[]&gt;();&lt;br /&gt;            int count = 0, length = 0;&lt;br /&gt;&lt;br /&gt;            MemoryStream ms = new MemoryStream(data);&lt;br /&gt;            DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);&lt;br /&gt;&lt;br /&gt;            while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) &gt; 0)&lt;br /&gt;            {&lt;br /&gt;                if (count == BUFFER_SIZE)&lt;br /&gt;                {&lt;br /&gt;                    tempList.Add(tempArray);&lt;br /&gt;                    tempArray = new byte[BUFFER_SIZE];&lt;br /&gt;                }&lt;br /&gt;                else&lt;br /&gt;                {&lt;br /&gt;                    byte[] temp = new byte[count];&lt;br /&gt;                    Array.Copy(tempArray, 0, temp, 0, count);&lt;br /&gt;                    tempList.Add(temp);&lt;br /&gt;                }&lt;br /&gt;                length += count;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            byte[] retVal = new byte[length];&lt;br /&gt;&lt;br /&gt;            count = 0;&lt;br /&gt;            foreach (byte[] temp in tempList)&lt;br /&gt;            {&lt;br /&gt;                Array.Copy(temp, 0, retVal, count, temp.Length);&lt;br /&gt;                count += temp.Length;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return retVal;&lt;br /&gt;        }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Jun 2007 22:03:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4129</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Sort generic list</title>
      <link>http://snippets.dzone.com/posts/show/4114</link>
      <description>&lt;code&gt;&lt;br /&gt;public class Item&lt;br /&gt;{&lt;br /&gt; public Item(string term, int freq)&lt;br /&gt; {&lt;br /&gt;  _term = term;&lt;br /&gt;  _freq = freq;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private string _term;&lt;br /&gt; public string Term&lt;br /&gt; {&lt;br /&gt;  get { return _term; }&lt;br /&gt;  set { _term = value; }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;  private int _freq;&lt;br /&gt;  public int Freq&lt;br /&gt;  {&lt;br /&gt;   get { return _freq; }&lt;br /&gt;   set { _freq = value; }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public class ItemComparer:IComparer&lt;Item&gt;&lt;br /&gt; {&lt;br /&gt;  #region IComparer&lt;Item&gt; Members&lt;br /&gt;&lt;br /&gt;  public int Compare(Item x, Item y)&lt;br /&gt;  {&lt;br /&gt;   return y.Freq - x.Freq; //descending sort&lt;br /&gt;   //return x.Freq - y.Freq; //ascending sort&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  #endregion&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;static void Main()&lt;br /&gt;{&lt;br /&gt; List&lt;Item&gt; items = new List&lt;Item&gt;();&lt;br /&gt; ....&lt;br /&gt; items.Sort(new ItemComparer());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Jun 2007 19:06:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4114</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>OnApplicationIdle event</title>
      <link>http://snippets.dzone.com/posts/show/4104</link>
      <description>&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Threading;&lt;br /&gt;using System.Reflection;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;public class HelloWorldForm : Form&lt;br /&gt;{&lt;br /&gt;    public HelloWorldForm()&lt;br /&gt;    {&lt;br /&gt;        Text = "Hello, WindowsForms!";&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;   &lt;br /&gt;public class ApplicationEventHandlerClass&lt;br /&gt;{&lt;br /&gt;    public void OnIdle(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine("The application is idle.");&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;   &lt;br /&gt;public class MainClass&lt;br /&gt;{&lt;br /&gt;    public static void Main()&lt;br /&gt;    {&lt;br /&gt;        HelloWorldForm FormObject = new HelloWorldForm();&lt;br /&gt;        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();&lt;br /&gt;   &lt;br /&gt;        Application.Idle += new EventHandler(AppEvents.OnIdle);&lt;br /&gt;        Application.Run(FormObject);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Jun 2007 11:39:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4104</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>OnApplicationExit event</title>
      <link>http://snippets.dzone.com/posts/show/4103</link>
      <description>&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Threading;&lt;br /&gt;using System.Reflection;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;public class HelloWorldForm : Form&lt;br /&gt;{&lt;br /&gt;    public HelloWorldForm()&lt;br /&gt;    {&lt;br /&gt;        Text = "Hello, WindowsForms!";&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;   &lt;br /&gt;public class ApplicationEventHandlerClass&lt;br /&gt;{&lt;br /&gt;    public void OnApplicationExit(object sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine("The application is shutting down.");&lt;br /&gt;        }&lt;br /&gt;        catch(NotSupportedException)&lt;br /&gt;        {&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;   &lt;br /&gt;public class MainClass&lt;br /&gt;{&lt;br /&gt;    public static void Main()&lt;br /&gt;    {&lt;br /&gt;        HelloWorldForm FormObject = new HelloWorldForm();&lt;br /&gt;        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();&lt;br /&gt;   &lt;br /&gt;        Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);&lt;br /&gt;        Application.Run(FormObject);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Jun 2007 11:38:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4103</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Reduce memory footprint of a .NET application</title>
      <link>http://snippets.dzone.com/posts/show/4093</link>
      <description>Don&#8217;t ask me anything. It just works :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; try&lt;br /&gt;{&lt;br /&gt;Process process = Process.GetCurrentProcess();&lt;br /&gt;process.MaxWorkingSet = loProcess.MaxWorkingSet;&lt;br /&gt;process.Dispose();&lt;br /&gt;}&lt;br /&gt;catch { }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;p.s. you can even make a timer that could run this piece of code periodically. </description>
      <pubDate>Sat, 02 Jun 2007 10:39:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4093</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Get list of running processes</title>
      <link>http://snippets.dzone.com/posts/show/4071</link>
      <description>&lt;code&gt;&lt;br /&gt; Console.WriteLine("ID:\tProcess name:");&lt;br /&gt; Console.WriteLine("--\t------------");&lt;br /&gt; foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())&lt;br /&gt;  Console.WriteLine("{0}\t{1}", process.Id, process.ProcessName); &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 29 May 2007 23:24:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4071</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Automatically send keys to other application</title>
      <link>http://snippets.dzone.com/posts/show/4070</link>
      <description>&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Diagnostics;&lt;br /&gt;&lt;br /&gt;namespace ConsoleApplicationTest&lt;br /&gt;{&lt;br /&gt;    class Program&lt;br /&gt;    {&lt;br /&gt;        [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]&lt;br /&gt;        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);&lt;br /&gt;        [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]&lt;br /&gt;        static extern bool SetForegroundWindow(IntPtr hWnd);&lt;br /&gt;&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            Process calc = Process.Start("calc.exe");&lt;br /&gt;            calc.WaitForInputIdle();&lt;br /&gt;&lt;br /&gt;            IntPtr calculatorHandle = FindWindow(null, "Calculator");&lt;br /&gt;            if (calculatorHandle == IntPtr.Zero)&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine("Calculator is not running.");&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                SetForegroundWindow(calculatorHandle);&lt;br /&gt;                SendKeys.SendWait("13");&lt;br /&gt;                SendKeys.SendWait("*");&lt;br /&gt;                SendKeys.SendWait("13");&lt;br /&gt;                SendKeys.SendWait("=");&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 29 May 2007 12:40:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4070</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>HTML Encoding/Decoding</title>
      <link>http://snippets.dzone.com/posts/show/4069</link>
      <description>&lt;code&gt;&lt;br /&gt; string temp = System.Web.HttpUtility.HtmlEncode("this is a test");&lt;br /&gt; temp2 = System.Web.HttpUtility.HtmlDecode(temp2);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;p.s. first add reference to "System.Web"</description>
      <pubDate>Mon, 28 May 2007 22:53:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4069</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>DateTime to double and vice versa</title>
      <link>http://snippets.dzone.com/posts/show/4068</link>
      <description>&lt;code&gt;&lt;br /&gt; public static double ToDouble(DateTime what)&lt;br /&gt; {&lt;br /&gt;  return BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static DateTime ToDateTime(double what)&lt;br /&gt; {&lt;br /&gt;  return new DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 May 2007 15:06:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4068</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Beep</title>
      <link>http://snippets.dzone.com/posts/show/4065</link>
      <description>&lt;code&gt;&lt;br /&gt;        [DllImport("kernel32.dll")]&lt;br /&gt;        public static extern bool Beep(int freq, int duration);&lt;br /&gt;&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            Beep(1000, 50);&lt;br /&gt;        }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 27 May 2007 22:07:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4065</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
  </channel>
</rss>
