Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Miroslav Stampar http://mstampar.awardspace.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 53 total  RSS 

Compress/decompress byte array

   1  
   2   using System;
   3   using System.Collections.Generic;
   4   using System.IO.Compression;
   5   using System.IO;
   6   using System.Collections;
   7  
   8  namespace Utilities
   9  {
  10   class Compression
  11   {
  12          public static byte[] Compress(byte[] data)
  13          {
  14              MemoryStream ms = new MemoryStream();
  15              DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
  16              ds.Write(data, 0, data.Length);
  17              ds.Flush();
  18              ds.Close();
  19              return ms.ToArray();
  20          }
  21          public static byte[] Decompress(byte[] data)
  22          {
  23              const int BUFFER_SIZE = 256;
  24              byte[] tempArray = new byte[BUFFER_SIZE];
  25              List<byte[]> tempList = new List<byte[]>();
  26              int count = 0, length = 0;
  27  
  28              MemoryStream ms = new MemoryStream(data);
  29              DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
  30  
  31              while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
  32              {
  33                  if (count == BUFFER_SIZE)
  34                  {
  35                      tempList.Add(tempArray);
  36                      tempArray = new byte[BUFFER_SIZE];
  37                  }
  38                  else
  39                  {
  40                      byte[] temp = new byte[count];
  41                      Array.Copy(tempArray, 0, temp, 0, count);
  42                      tempList.Add(temp);
  43                  }
  44                  length += count;
  45              }
  46  
  47              byte[] retVal = new byte[length];
  48  
  49              count = 0;
  50              foreach (byte[] temp in tempList)
  51              {
  52                  Array.Copy(temp, 0, retVal, count, temp.Length);
  53                  count += temp.Length;
  54              }
  55  
  56              return retVal;
  57          }
  58   }
  59  }

Sort generic list

   1  
   2  public class Item
   3  {
   4   public Item(string term, int freq)
   5   {
   6    _term = term;
   7    _freq = freq;
   8   }
   9  
  10   private string _term;
  11   public string Term
  12   {
  13    get { return _term; }
  14    set { _term = value; }
  15   }
  16  
  17    private int _freq;
  18    public int Freq
  19    {
  20     get { return _freq; }
  21     set { _freq = value; }
  22    }
  23   }
  24  
  25   public class ItemComparer:IComparer<Item>
  26   {
  27    #region IComparer<Item> Members
  28  
  29    public int Compare(Item x, Item y)
  30    {
  31     return y.Freq - x.Freq; //descending sort
  32     //return x.Freq - y.Freq; //ascending sort
  33    }
  34  
  35    #endregion
  36   }
  37  
  38  static void Main()
  39  {
  40   List<Item> items = new List<Item>();
  41   ....
  42   items.Sort(new ItemComparer());
  43  }
  44  

OnApplicationIdle event

   1  
   2  using System;
   3  using System.Threading;
   4  using System.Reflection;
   5  using System.Windows.Forms;
   6     
   7  
   8  public class HelloWorldForm : Form
   9  {
  10      public HelloWorldForm()
  11      {
  12          Text = "Hello, WindowsForms!";
  13      }
  14  }
  15     
  16  public class ApplicationEventHandlerClass
  17  {
  18      public void OnIdle(object sender, EventArgs e)
  19      {
  20          Console.WriteLine("The application is idle.");
  21      }
  22  }
  23     
  24  public class MainClass
  25  {
  26      public static void Main()
  27      {
  28          HelloWorldForm FormObject = new HelloWorldForm();
  29          ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
  30     
  31          Application.Idle += new EventHandler(AppEvents.OnIdle);
  32          Application.Run(FormObject);
  33      }
  34  }

OnApplicationExit event

   1  
   2  using System;
   3  using System.Threading;
   4  using System.Reflection;
   5  using System.Windows.Forms;
   6     
   7  
   8  public class HelloWorldForm : Form
   9  {
  10      public HelloWorldForm()
  11      {
  12          Text = "Hello, WindowsForms!";
  13      }
  14  }
  15     
  16  public class ApplicationEventHandlerClass
  17  {
  18      public void OnApplicationExit(object sender, EventArgs e)
  19      {
  20          try
  21          {
  22              Console.WriteLine("The application is shutting down.");
  23          }
  24          catch(NotSupportedException)
  25          {
  26          }
  27      }
  28  }
  29     
  30  public class MainClass
  31  {
  32      public static void Main()
  33      {
  34          HelloWorldForm FormObject = new HelloWorldForm();
  35          ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
  36     
  37          Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);
  38          Application.Run(FormObject);
  39      }
  40  }

Reduce memory footprint of a .NET application

Don’t ask me anything. It just works :)

   1  
   2   try
   3  {
   4  Process process = Process.GetCurrentProcess();
   5  process.MaxWorkingSet = loProcess.MaxWorkingSet;
   6  process.Dispose();
   7  }
   8  catch { }


p.s. you can even make a timer that could run this piece of code periodically.

Get list of running processes

   1  
   2   Console.WriteLine("ID:\tProcess name:");
   3   Console.WriteLine("--\t------------");
   4   foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
   5    Console.WriteLine("{0}\t{1}", process.Id, process.ProcessName); 

Automatically send keys to other application

   1  
   2  using System;
   3  using System.Collections.Generic;
   4  using System.Text;
   5  using System.Windows.Forms;
   6  using System.Diagnostics;
   7  
   8  namespace ConsoleApplicationTest
   9  {
  10      class Program
  11      {
  12          [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  13          static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  14          [System.Runtime.InteropServices.DllImport("USER32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  15          static extern bool SetForegroundWindow(IntPtr hWnd);
  16  
  17          static void Main(string[] args)
  18          {
  19              Process calc = Process.Start("calc.exe");
  20              calc.WaitForInputIdle();
  21  
  22              IntPtr calculatorHandle = FindWindow(null, "Calculator");
  23              if (calculatorHandle == IntPtr.Zero)
  24              {
  25                  Console.WriteLine("Calculator is not running.");
  26              }
  27              else
  28              {
  29                  SetForegroundWindow(calculatorHandle);
  30                  SendKeys.SendWait("13");
  31                  SendKeys.SendWait("*");
  32                  SendKeys.SendWait("13");
  33                  SendKeys.SendWait("=");
  34              }
  35          }
  36      }
  37  }

HTML Encoding/Decoding

   1  
   2   string temp = System.Web.HttpUtility.HtmlEncode("this is a test");
   3   temp2 = System.Web.HttpUtility.HtmlDecode(temp2);


p.s. first add reference to "System.Web"

DateTime to double and vice versa

   1  
   2   public static double ToDouble(DateTime what)
   3   {
   4    return BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
   5   }
   6  
   7   public static DateTime ToDateTime(double what)
   8   {
   9    return new DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));
  10   }

Beep

   1  
   2          [DllImport("kernel32.dll")]
   3          public static extern bool Beep(int freq, int duration);
   4  
   5          static void Main(string[] args)
   6          {
   7              Beep(1000, 50);
   8          }
« Newer Snippets
Older Snippets »
Showing 1-10 of 53 total  RSS