Friday, July 12, 2013

WPF - Different ways to get System IP Address

Following method get the local from the list of Addresses as it has more IP address if it has more adapters

private object GetLocalIPAddressFromAddressList()
        {
            var hostName = Dns.GetHostName();
            var ipEntry = Dns.GetHostEntry(hostName);

            var result = from address in ipEntry.AddressList
                    where address.AddressFamily == AddressFamily.InterNetwork
                    select address;
            return result.LastOrDefault();

        }


Using Ping method to get the IP Address, it helpful to find others IP address also

        public IPAddress GetIPAddressUsingPing()
        {
            string hostName = "rbalajiprasad.blogspot.com";
            //You can get local Computer IP address also by sending HostName by following
            //string hostName = System.Net.Dns.GetHostName();


            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

Thursday, July 11, 2013

WPF - Export to excel using EPPlus

Following code helpful to create a excel file from WPF

Prerequesties:-
EPPlus DLL   (Ref: http://epplus.codeplex.com/releases/view/89923)

Step 1: Create a WPF project

Step 2:  Add reference of EPPlus DLL

Step 3: Following is the sample class to export

        public class Employee
        {
            public string EmployeeName;
            public int EmployeeAge;
            public string Designation;
        }


Step 4: Create Data with values

            List<Employee> lstEmployee = new List<Employee>();
            lstEmployee.Add(new Employee() { EmployeeName = "Balaji", EmployeeAge = 16, Designation = "DE" });
            lstEmployee.Add(new Employee() { EmployeeName = "prasad", EmployeeAge = 16, Designation = "TA" });

           
Step 5: Create Header Data for excel
           List<string> lstHeader = new List<string>() { "Employee Name", "Employee Age", "Designation" };
Step 6: Create Excel Package and add Header and Column details

            ExcelPackage pck = new ExcelPackage();
            pck.Workbook.Properties.Author = "Balajiprasad";
            pck.Workbook.Properties.Title = "EPPlus in WPF";
            pck.Workbook.Properties.Company = "For Aditi Technologies";

            var ws = pck.Workbook.Worksheets.Add("Employee Details");

            //Header Section
            for (int i = 0; i < lstHeader.Count; i++)
            {
                ws.Cells[1, i + 1].Value = lstHeader[i];
                ws.Cells[1, i + 1].Style.Font.Bold = true;
            }

            //Column Value Section
            for (int i = 0; i < lstEmployee.Count; i++)
            {
                ws.Cells[i + 2, 1].Value = lstEmployee[i].EmployeeName;
                ws.Cells[i + 2, 2].Value = lstEmployee[i].EmployeeAge;
                ws.Cells[i + 2, 3].Value = lstEmployee[i].Designation;
            }


Step 7: Save as the byte array as excel

            byte[] fileText = pck.GetAsByteArray();

            SaveFileDialog dialog = new SaveFileDialog()
            {
                Filter = "Excel Worksheets (*.xlsx)|*.xlsx"
            };

            if (dialog.ShowDialog() == true)
            {
                File.WriteAllBytes(dialog.FileName, fileText);
            }


Following is the entire code

        private void GenerateExcel()
        {
            List<Employee> lstEmployee = new List<Employee>();
            lstEmployee.Add(new Employee() { EmployeeName = "Balaji", EmployeeAge = 16, Designation = "DE" });
            lstEmployee.Add(new Employee() { EmployeeName = "prasad", EmployeeAge = 16, Designation = "TA" });

            List<string> lstHeader = new List<string>() { "Employee Name", "Employee Age", "Designation" };


            ExcelPackage pck = new ExcelPackage();
            pck.Workbook.Properties.Author = "Balajiprasad";
            pck.Workbook.Properties.Title = "EPPlus in WPF";
            pck.Workbook.Properties.Company = "For Aditi Technologies";

            var ws = pck.Workbook.Worksheets.Add("Employee Details");

            //Header Section
            for (int i = 0; i < lstHeader.Count; i++)
            {
                ws.Cells[1, i + 1].Value = lstHeader[i];
                ws.Cells[1, i + 1].Style.Font.Bold = true;
            }

            //Column Value Section
            for (int i = 0; i < lstEmployee.Count; i++)
            {
                ws.Cells[i + 2, 1].Value = lstEmployee[i].EmployeeName;
                ws.Cells[i + 2, 2].Value = lstEmployee[i].EmployeeAge;
                ws.Cells[i + 2, 3].Value = lstEmployee[i].Designation;
            }

            byte[] fileText = pck.GetAsByteArray();

            SaveFileDialog dialog = new SaveFileDialog()
            {
                Filter = "Excel Worksheets (*.xlsx)|*.xlsx"
            };

            if (dialog.ShowDialog() == true)
            {
                File.WriteAllBytes(dialog.FileName, fileText);
            }

        }


public class Employee
        {
            public string EmployeeName;
            public int EmployeeAge;
            public string Designation;
        }



Monday, June 24, 2013

C# - How to use escape characters in visual studio summary comments

To write the code examples in VS use CDATA and it will avoid warnings from StyleCop

Below is example

/// <summary>
/// How to use this method: <![CDATA[ new List<object>() { true, 100 } ]]>
/// </summary>

Monday, May 13, 2013

C# - Delegate Find method using Predicate


Delegate Find method using Predicate
static Predicate<Car> ByYear(int year)
{
    return delegate(Car car)
    {
        return car.Year == year;
    };
}

static void Main(string[] args)
{
    
    List<Car> list = new List<Car>
    {
        new Car { Year = 1940 },
        new Car { Year = 1965 },
        new Car { Year = 1973 },
        new Car { Year = 1999 }
    };
    var car99 = list.Find(ByYear(1999));
    var car65 = list.Find(ByYear(1965));

    Console.WriteLine(car99.Year);
    Console.WriteLine(car65.Year);
}

Thursday, April 18, 2013

Visual Studio - How to run as administrator by default

To run Visual studio as administrator by default do the following steps,


Step 1: Go to VS2012 (Any version) shortcut place
    Ex: "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2012"

Step 2: Right click on "Visual Studio 2012" icon

Step 3: Click properties

Step 4:  On properties windows click Advanced


Step 5:  Check "Run as administrator" and Click OK





Wednesday, April 17, 2013

.Net - Solved - How to load assembly without error

Sometimes we will get error message like below

Could not load the assembly file://\assemblypath/assemblyname.dll. This assembly may have been downloaded from the Web. If an assembly has been downloaded from the Web, it is flagged by Windows as being a Web file, even if it resides on the local computer. This may prevent it from being used in your project. You can change this designation by changing the file properties. Only unblock assemblies that you trust. See http://go.microsoft.com/fwlink/?LinkId=179545 for more information.

To solve this issue right click the assembly dll and click unblock