9:56 AM

Calculate the execution time of a method

using System; using System.Diagnostics; using System.Threading; class Program { static void Main() { // Create new stopwatch Stopwatch stopwatch = new Stopwatch(); // Begin timing stopwatch.Start(); // Do something for (int i = 0; i < 1000; i++) { Thread.Sleep(1); } // Stop timing stopwatch.Stop(); // Write result Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); } } A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals [.....]

10:05 AM

How to get current line number

         static void Main(string[] args)         {             try             {                                throw new Exception();             }             catch (Exception ex)             {                 // Get stack trace for the exception with source file information                 var trace = new StackTrace(ex, true);
                // Get the top stack frame     [.....]

10:48 PM

Singleton

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
} [.....]

10:42 PM

Multithreaded Singleton

using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get
      {
         if (instance == null)
         {
            lock (syncRoot)
            {
               if (instance == null)
                  instance = new Singleton();
            }
         }

         return instance;
      }
[.....]

1:24 AM

Xml document to xdocument

         private static XDocument XMLDocumentToXDocument(XmlDocument doc)
        {
            return XDocument.Parse(doc.OuterXml);
        } [.....]

12:46 AM

Compress and Decompress

       public static string Compress(string s)
       {
           var bytes = Encoding.Unicode.GetBytes(s);
           using (var msi = new MemoryStream(bytes))
           using (var mso = new MemoryStream())
           {
               using (var gs = new GZipStream(mso, CompressionMode.Compress))
               {
                   msi.CopyTo(gs);
               }
               return Convert.ToBase64String(mso.ToArray());
           }
       }
       public static string Decompress(string s)
    [.....]

3:42 AM

String array in C#

List ls = new List(); foreach (string line in assignment_lines) { ls.AddRange(line.Split('=')); } string[] strArray = ls.ToArray(); [.....]

4:12 AM

Get application physical base directory path in WCF Service

               var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "xml";
                XmlDocument xmlDoc = new XmlDocument();
                XDocument xDoc = new XDocument();
                string bodyFile = Path.Combine(appPath, @"Destinations.xml");
                //xmlDoc.Load(bodyFile);
                xDoc = XDocument.Parse(bodyFile); [.....]

9:55 PM

Read xml

http://forums.asp.net/t/1893828.aspx/1?read+xml





[.....]

11:51 PM

Remove NameSpace In XML Document


 public static string RemoveNamespace(XmlDocument xdoc)
        {
            // Regex to search for either xmlns:nsN="..." or xmlns="..."
            const string pattern = @"xmlns:?(\w+)?=""[\w:/.]+""";
            string xml = xdoc.OuterXml;

            Regex replace = new Regex(pattern);
            // Replace all occurances of the namespace declaration
            string temp = replace.Replace(xml, String.Empty);
            foreach (Match match in replace.Matches(xml))
            {
                // Loop through each Match in the Regex, [.....]

11:44 PM

random string generator


 public string AutoGenerateString(int lenght)
        {
            string def = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";//0123456789";
            Random rnd = new Random();
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < lenght; i++)
            {
                ret.Append(def.Substring(rnd.Next(def.Length), 1));
            }
            return ret.ToString();
        }
[.....]

4:59 AM

popup display





 
  jQuery UI Dialog - Modal message

 

 

 


        $(function () {           $("#create-user")       .button()       .click(function () {           $("p").css('display', 'block');           $("#dialog-message").dialog({               modal: true,               buttons: {                   Ok: function () {                       $(this).dialog("close");                   }             [.....]

6:47 AM

C# Split A String By Another String


string data = "India, officially the Republic of India (Bharat Ganrajya)[c], is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world ";

return data.Split(new string[] { "populous" }, StringSplitOptions.None); [.....]

12:18 AM

combine two xml files into one

xml1.xml
----------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test1</NodeB>
      <NodeB>test2</NodeB>
   </NodeA>
</AllNodes>





xml2.xml
-------------------



<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test6</NodeB>
      <NodeB>test7</NodeB>
   </NodeA>
   <NodeA>
      <NodeB>test99</NodeB>
      <NodeB>test23</NodeB>
   </NodeA>
</AllNodes>



using LINQ to XML



var xml1 = XDocument.Load("xml1.xml");
var xml2 = XDocument.Load("xml2.xml");

//Combine and remove duplicates
var combinedUnique = xml1.Descendants("AllNodes")
                          .Union(xml2.Descendants("AllNodes"));

//Combine and keep duplicates
var combinedWithDups = xml1.Descendants("AllNodes")
                [.....]

3:13 AM

Get Triggers ALL in SQL SERVER 2008


 List all triggers(including database level) in the database - QUICK SYNTAX SELECT *  FROM [AdventureWorks2008].[sys].[triggers] [.....]

Next PostNewer Posts Previous PostOlder Posts Home