Sunday, April 25, 2010

Creating Single instance of a windows Form

//Following Code in Child form Class 
//creating form's object.
public static volatile System_Categorization singleObj;

        
// Function in the Form
public static System_Categorization singleInstance()
 {
 if (singleObj == null)
  {
    lock (typeof(System_Categorization))
     {
       if (singleObj == null)
        {
          singleObj = new System_Categorization();
        }
     }
  }
return singleObj;
}

//On Disposing please Please Write following code in Designer class 

protected override void Dispose(bool disposing)
 {
   //mainform objmainform = new mainform();
            
     if (disposing && (components != null))
       {
         components.Dispose();
       }
       base.Dispose(disposing);
     singleObj = null;
           
 }
 
//Following Code in Parent form Class 
// Now open the form as write code in Parent Form
 
 //System_Categorization objForm = new System_Categorization();
System_Categorization.singleInstance().Show(); 

Creating Single Instance of Your Application

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace myappliaction
{
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "myappliaction", out createdNew))
{

try
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainform());
}
else
{
MessageBox.Show("Application is already running.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//Process current = Process.GetCurrentProcess();
//foreach (Process process in Process.GetProcessesByName(current.ProcessName))
//{
// if (process.Id != current.Id)
// {
// SetForegroundWindow(process.MainWindowHandle);
// break;
// }
//}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
}
}

Drawing Pie Chart

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;

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

private void btn_show_Click(object sender, EventArgs e)
{
// Create a Graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);

// get the current value of start and sweep angles
float startAngle = (float)Convert.ToDouble(textBox1.Text);
float sweepAngle = (float)Convert.ToDouble(textBox2.Text);

// Create a pen
Pen writePen = new Pen(Color.Blue, 1);

// Draw pie
g.DrawPie(writePen, 20, 20, 100, 100, startAngle, sweepAngle);

// dispose of objects
writePen .Dispose();
g.Dispose();
}
}
}

Tuesday, April 20, 2010

Getting Computer Names from Domain using C#

ArrayList ALExist = new ArrayList();
ArrayList AL = new ArrayList();
DataTable dt_Domainsystem_withIP = new DataTable();



public void get_computername()
{
   //Invoke(new MyDelegate(ShowProgressBar), false);
   //string server = ""; //Insert Domain controller server here
   string server = Environment.UserDomainName; //Insert Domain controller server here
   string adminUser = Environment.UserDomainName + Environment.UserName;//Insert an admin user account here
   //string adminUser = ""; 
    string adminPass = ""; //Password for above username
    DirectoryEntry de = new DirectoryEntry();
    de.Path = "LDAP://" + server + "";
    de.Username = adminUser;
    de.Password = adminPass;
            

    try
    {
                float progress = 0;
                DirectorySearcher ser = new DirectorySearcher();
                ser.Filter = "(&ObjectCategory=computer)"; //Only allows Computers to be returned in results.
                SearchResultCollection results = ser.FindAll();
                foreach (SearchResult res in results)
                {
                    string[] temp = res.Path.Split(','); //temp[0] would contain the computer name ex: cn=computerName,..
                    AL.Add(temp[0].Substring(10).ToString());//returns everything after LDAP://CN= until end of temp[0].
                }
                //c1_doaminSystems.DataSource = AL;
                //c1_doaminSystems.Rebind(true);
                //DataSet ds = new DataSet();

                AL.Sort();
                lbl_total_domain.Text = AL.Count.ToString();
                
                try
                {
                    dt_Domainsystem_withIP.Columns.Add("Computer Name", typeof(string));
                    dt_Domainsystem_withIP.Columns.Add("IP Address", typeof(string));
                }
                catch (Exception ex)
                { }



                for (int i = 0; i < AL.Count; i++)
                {

                    dr = dt_Domainsystem_withIP.NewRow();
                    dr[0] = AL[i].ToString();

                    try
                    {
                        IPAddress[] addresslist = Dns.GetHostAddresses(AL[i].ToString());
                        foreach (IPAddress theaddress in addresslist)
                        {
                            dr[1] = theaddress.ToString();
                        }
                        ALExist.Add(AL[i].ToString());
                    }
                    catch (Exception ex)
                    {
                        //ALIPException.Add(AL[i].ToString());
                        dr[1] = ex.Message.ToString();
                    }


                    dt_Domainsystem_withIP.Rows.Add(dr);
                    float divide = (float) i / AL.Count;
                    progress = (divide*100);

                    int total_progess = (int)progress;

                    if (total_progess % 2 == 0)
                    {
                        lbl_progress.Text = total_progess.ToString() + "% Complete..";
                    }
                    else
                        lbl_progress.Text = total_progess.ToString() + "% Complete....";


                }
                Grid.DataSource = dt_Domainsystem_withIP;
                Grid.Rebind(true);
                
                


                








            }

            catch (Exception ex)
            {

               // Console.WriteLine(ex.ToString());

            }

            finally
            {

                de.Dispose();//Clean up resources

            }



        }