Sunday, November 28, 2010

Read SQl Server Name

using Microsoft.SqlServer.Management.Smo;

protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                Response.Write(dr["Name"].ToString() + "
");
            }
        }
    }
 
Note: You have to add a Reference of Microsoft.SqlServer.Smo dll. 

Unique Identifier for a computer

There are so many unique identifier. You may use Processor ID, any device Serial No. like Hard Drive Serial No. etc.

Please check the following code to read the ProcessorID:

public string GetMachineId()
    {
        string cpuInfo = String.Empty;
        ManagementClass managementClass = new ManagementClass("Win32_Processor");
        ManagementObjectCollection managementObjCol = managementClass.GetInstances();
        foreach (ManagementObject managementObj in managementObjCol)
        {
            if (cpuInfo == String.Empty)
            {
                cpuInfo = managementObj.Properties["ProcessorId"].Value.ToString();
            }
        }
        return cpuInfo;
    }


Note: You must add a reference of System.Management Assembly to use the above program.

Thursday, November 25, 2010

How to find the system is connected in LAN or not ?

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
      
 
 
//Gets the machine names that are connected on LAN  
  
Process netUtility = new Process();  
  
netUtility.StartInfo.FileName = "net.exe";  
  
netUtility.StartInfo.CreateNoWindow = true;  
  
netUtility.StartInfo.Arguments = "view";  
  
netUtility.StartInfo.RedirectStandardOutput = true;  
  
netUtility.StartInfo.UseShellExecute = false;  
  
netUtility.StartInfo.RedirectStandardError = true;  
  
netUtility.Start();  
  
   
  
StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream, netUtility.StandardOutput.CurrentEncoding);  
  
   
  
string line = "";  
  
while ((line = streamReader.ReadLine()) != null)  
  
{  
  
      if (line.StartsWith("\\"))  
  
      {  
  
           listBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());  
  
      }  
  
}  
  
streamReader.Close();  
netUtility.WaitForExit(1000);  


        }
    }
}