Thursday, January 26, 2017

C# Code


*********************************************
String interpolation
https://www.dotnetperls.com/string-interpolation

--instead of first line we can also write as shown in second line

var folder = string.Format("c:\\deploy\\{0}\\", outputFolder);
var folder = $@"c:\deploy\{outputFolder}\";

*********************************************************
uninstall application using C# code

in command prompt
C:\Users\inpkumar>wmic product GET identifyingnumber, name, version /FORMAT:"C:\Windows\System32\wbem\en-US\csv.xsl"

In command prompt:
wmic product where "Name like '%CALIGO X64%'" get identifyingnumber, Name, Version

msiexec.exe /x {your-product-code-guid}
https://stackoverflow.com/questions/17523974/how-to-uninstall-msi-using-its-product-code-in-c-sharp

*******************************************************************
Stopping and Starting Windows services:
services.msc


using System.ServiceProcess;

namespace testsuite.functions
{
    class ServicesWindows_msc
    {

     static ServiceController[] scServices;                

      public static void StartService(string serviceName)
        {
            scServices = ServiceController.GetServices();

            //Start
            foreach (var item in scServices)
            {
                if (serviceName.Equals(item.ServiceName))
                {

                    if (item.Status == ServiceControllerStatus.Stopped)
                    {
                        item.Start();
                        item.WaitForStatus(ServiceControllerStatus.Running);
                    }
                }
            }          
        }

        public static void StopService(string serviceName)
        {
            scServices = ServiceController.GetServices();
                                             
            foreach (var item in scServices)
            {
                if (serviceName.Equals(item.ServiceName))
                {

                    if (item.Status == ServiceControllerStatus.Running)
                    {
                        item.Stop();
                        item.WaitForStatus(ServiceControllerStatus.Stopped);
                    }
                }
            }
        }
    }
}
   
**************************************************************
Comparing images


ImageComparer.Compare Method (Image, Image, ColorDifference, Image)


https://msdn.microsoft.com/en-us/library/hh191601.aspx

********************************************************************************************

Deleting Windows Registry entry program in C# (Command Prompt)

ProcessStartInfo procStartInfo = new ProcessStartInfo();
        procStartInfo.FileName = "cmd.exe";
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        string inputArgs = “HKCU\Software\Zeiss\Anything”;
        string argum = string.Format("/C REG DELETE {0} /f", arguments);
        procStartInfo.Arguments = argum;
        procStartInfo.Verb = "runas";
        Process.Start(procStartInfo);

***********************************************************************

Debug vs Release

Debug 
code is not optimised, used by developers. Stack trace information will be very specific and contains all methods to reach the error point.

Release :
Fast.
Stack trace will be Nil.

in Release the following code wont get hit
#if DEBUG
                Console.WriteLine("Somethings");

             #endif

some more information:

  • #if DEBUG: The code in here won't even reach the IL on release.
  • [Conditional("DEBUG")]: This is a  method Attribute. This code will reach the IL, however calls to the method will be omitted unless DEBUG is set when the caller is compiled. 


Delegates:

https://www.youtube.com/watch?v=AVHk97amJ4I

namespace HelloWorld
{
    public delegate void MyDelegat_XX (string Name);

    public class _DelegateClass
    {
        public MyDelegat_XX myDel_XX;
    }

    class ProgramClass
    {
        static void Main(string[] args)
        {
            // In Delegates, we can assign and invoke the delegate out the
            //class (ProgramClass) where the delegates is declared-(_DelegateClass)
            _DelegateClass newClassOjb = new _DelegateClass();
            newClassOjb.myDel_XX = Print_Method;
            newClassOjb.myDel_XX("Pradeep Kumar");
        }

        static void Print_Method(string Name)
        {
            Console.WriteLine(Name);
        }
    }

}






Events & Delegates:

Events is the abstration layer over the delegate
namespace HelloWorld
{
    public delegate void MyDelegat_XX (string Name);

    public class _DelegateClass
    {
        //use event before the Delegate to add encapuslation to the delegate
        public event MyDelegat_XX myDel_XX;

        public void _InvokeXX_Method()
        {
            myDel_XX("MyNameIsPradeep");
        }
    }

    class ProgramClass
    {
        static void Main(string[] args)
        {     
            //1. After adding event keyword, we cannot assign to or invoke delegate
            // We can only add/ remove method
            _DelegateClass newClassOjb = new _DelegateClass();

            //2 We can add or remove a method
            newClassOjb.myDel_XX += Print_Method;
           
            newClassOjb.myDel_XX += Display_Method;

            newClassOjb._InvokeXX_Method();
            Console.WriteLine("----------------------------------");

            newClassOjb.myDel_XX -= Display_Method;

            newClassOjb._InvokeXX_Method();
        }

        static void Print_Method(string Name)
        {
            Console.WriteLine(Name);
        }

        static void Display_Method(string Name)
        {
            Console.WriteLine(Name+" From Display Method");
        }
    }
}


Generic Example

class Program
{
    static void Main(string[] args)
    {
        MyClass<int> vb1 = new MyClass<int>();
        Console.WriteLine(vb1.Call(112));
        MyClass<string> vb2 = new MyClass<string>();
        Console.WriteLine(vb2.Call("Hello"));
    }
}

class MyClass<xyzName>
{
    public xyzName Call(xyzName vb)
    {
        return vb;
    }
}



Interface Example:


class InterfaceExample
{
    public void Method()
    {
        IFirst tc1 = new TestClass();
        ISecond tc2 = new TestClass();

        tc1.Show();
        tc2.Show();
    }
}
public interface IFirst
{
    void Show();
}
public interface ISecond
{
    void Show();
}
public class TestClass : IFirst, ISecond
{
    void IFirst.Show()
    {
        Console.WriteLine("This is ITest");
    }

    void ISecond.Show()
    {
        Console.WriteLine("This is ISecond");
    }

}



*****************************************************************************





using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> li = new List<string>();

            Assembly assembly = Assembly.LoadFile(@"F:\Caligo-SourceRoot\1Mainline-win7\CaligoAutoWin7Main\bin\Debug\CaligoAutoWin7Main.dll");

            Type type = assembly.GetType("CaligoAutoWin7Main.CaligoTest");
            MethodInfo[] mi = type.GetMethods();
            foreach (MethodInfo method in mi)
            {
                object attribute = method.GetCustomAttributes(typeof(TestMethodAttribute)).FirstOrDefault();

                if (attribute != null)
                {
                    li.Add(method.Name);
                }
            }
        }

    }

}

//Make sure the Microsoft.VisualStudio.QualityTools.UnitTestFramework reference is having the same version of both the Referenced(TestProject)  and the referencing Project.


https://social.msdn.microsoft.com/Forums/vstudio/en-US/18ccce46-8e18-42ef-8b1f-ba9a3155fc29/how-to-get-all-the-test-method-name-from-the-test-project?forum=vstest



***********************************************************************



Remove readonly attribute from directory and delete directory

https://stackoverflow.com/questions/611921/how-do-i-delete-a-directory-with-read-only-files-in-c


public static void ForceDeleteDirectory(string path)
        {
            if(Directory.Exists(path))
            {
            var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };

            foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
            {
                info.Attributes = FileAttributes.Normal;
            }
            
            //Log deleting all files recursively from path
            directory.Delete(true);

            }
            else
            {
                //Log Folder does not exist
            }
        }



JMeter Simple Controller

  Simple Controller is just a  container  for user request.