Wednesday, September 25, 2013

Get installed application list in windows OS using C#.Net


Find installed application in my PC using C#.Net

Some time we need to know before installing our new setup file it’s already installed or not in the computer. In the below examples I have explained how to get list of application installed in the system (OS) using C#.Net 2.0. The Example 1 will give you more quick result compare then Example 2.

  private void Form1_Load(object sender, EventArgs e)
        {
            CheckInstalledApplication();
            LoadSoftwareList();
        }

In the Example1 explained get the registory access and display the list of installed application in the windows system.

Example 1:
//Check the list of application installed in the OS
        private bool CheckInstalledApplication()
        {
            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            string strDispName;
            List<string> lsRegistory = new List<string>();
            using (RegistryKey rkRegistryKeyOne = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {
                foreach (object varName in rkRegistryKeyOne.GetSubKeyNames())
                {
                    using (RegistryKey rkRegistryKeyTwo = rkRegistryKeyOne.OpenSubKey(varName.ToString()))
                    {
                        strDispName = Convert.ToString(rkRegistryKeyTwo.GetValue("DisplayName"));
                        if (!string.IsNullOrEmpty(strDispName))
                        {
                            lsRegistory.Add(strDispName + " ---> " + Convert.ToString(rkRegistryKeyTwo.GetValue("DisplayVersion")));
                        }
                    }
                }
            }           
            lsRegistory.Sort();           
            listBox1.DataSource = lsRegistory.ToArray();
            return true;            
        }

In the Example2 explained using management object get the win32 product list from the windows OS.

Example 2:
using System.Management;
        private void LoadSoftwareList()
        {
            listBox1.Items.Clear();
            ManagementObjectCollection moReturn;
            ManagementObjectSearcher moSearch;

            moSearch = new ManagementObjectSearcher("Select * from Win32_Product");

            moReturn = moSearch.Get();
            foreach (ManagementObject mo in moReturn)
            {
                listBox1.Items.Add(mo["Name"].ToString());
            }

        } 
Note: Thanks to Antony

Saturday, September 21, 2013

Read Data From Excel files in C#.Net



Read Data From Excel files in C#.Net:
The below sample code will help you to get data from excel sheet. You can use same code in both Winforms and web applications.  Different type of provider available to read different type of the excel version. Here I have given two type of provider to read excel sheet. One is old file format XLS excel file reader (Excel 2007) and another one is latest file format of XLSX file reader (Excel 2010).
using System.Data.OleDb;
 
 //Reading from XLS file (Office 2007 and before)
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FileName.xls;Extended Properties='Excel 8.0;HDR=Yes;'");
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Reading from XLSX file (Office 2007 & After)
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con = new OleDbConnection(@"Provider= Microsoft.ACE.OLEDB.12.0;Data Source=D:\FileName.xlsx; Extended Properties='Excel 12.0;HDR=YES;'"); 
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

The above given example of reading my local hard drives excel sheets file first page data to my C# data set. If you want to change sheet you can specify the select statement [Sheet2$].
Thanks to for giving this example  Mr.Antony.

Thursday, September 19, 2013

most frequently used sql queries




In the SQL query frequently most of the developers used the below query’s for day to day needs.


Get the top Nth value in the table.
--Get top 3 rd value
select min (salary) from EmpTable where Salary in (select top 3 salary from EMPTable)

How to select duplicate records in SQL.

--Select Duplicate value
select CName,count(*) from MyTable group by CName having count(*) >1

How to DELETE duplicate value from the Data TABLE using SQL Query.

--Delete dublicate value
delete from MyTable where ID not in(select MIN(ID) from MYTable group  by CName)