Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, February 5, 2011

How to make program to get device drivers - Part 2

In order to get device drivers which installed on your computer we will write method its name (GetDriversList), this method is it most important part of our program which it consists of sub-method its name (GetDriver), which get and store driver informaion.
So the first stage is to bring a list containing the device drivers installed on your computer, which, as we mentioned previously that it found in the Registry Editor, and then brought the information contained in the key of each driver.
 
To store information about each driver, you declare the structure (DriverInfo) containing 6 fields to store the name, version, date, manufacture ....... of each driver.
 
The method that bring in device drivers (GetDriversList), supplied with a parameter determines if we want to get all the drivers, including drivers that are installed with the system or only drivers that you installed after you install the system. In order to get that we test the value of the manufacturer (drvManufacture) If it is equal to (Microsoft), this means that the driver was already installed with the system and we do not need to do a backup copy of it.

struct DriverInfo  //structure for storing driver informations
{
    public string drvName;
    public string drvVersion;
    public string drvManufacture;
    public string drvDate;
    public string drvInfPath;
    public string drvInfSection;
}

Thursday, January 27, 2011

How to show line numbers in Visual Studio.NET

How to show line numbers:
In order to show line numbers in the program Visual Studio. Net as it appears in following image:


1. Open the Tools menu and then choose Options
2. Then from the left side expand the node Text Editor
3. Then choose the node All Languages
4. Then check the Line Numbers

Saturday, January 22, 2011

C# Codes

Create a directory if it does not exist

    if (!Directory.Exists(location))
        
    Directory.CreateDirectory(location);
     How to read the hard drive serial number

      public string GetHDDSerialNumber(string drive)

          
      if (drive == "" || drive == null)
          {
              
      drive "C";
          }  


          
      ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" drive +":\"");
          
      disk.Get();
         

          
      return disk["VolumeSerialNumber"].ToString();
      }

      Get a random number between two concrete

        Random Rnd = new Random();
        int A Rnd.Next(010); 
        Change the keyboard language to Arabic or English
          public enum Languge int{
              
          Arabic 0,
              
          English 1}

          public static 
          void SetLang(Languge Lang)
          {
              try
              {
                  
          string MyLang;
                  if (
          Lang == Languge.Arabic)
                      
          MyLang "Arabic";
                  else
                      
          MyLang "English";

                  foreach (
          InputLanguage ThisLang in InputLanguage.InstalledInputLanguages)
                      if (
          ThisLang.Culture.EnglishName.Contains(MyLang))
                          
          InputLanguage.CurrentInputLanguage ThisLang;
              }
              catch { }
          }
           
          To get the number of seconds that passed the device starts

            Int64 SecondsFromStart Environment.TickCount/1000;

            Way of sending e-mail from your porgram

              using System.ServiceProcess;  
              using System.Net.Mail;  
              using System.Text;  
              using System.Collections
              using System

              namespace MOJ {
                  class 
              Email
                  
              {
                      public 
              bool SendEmail(MailAddress FromEmailMailAddress ToEmailstring SubjectMailAddressCollection ToCCMailAddressCollection ToBCCstring Bodystring SMTPbool IsHTMLArrayList Attachments)
                      {
                          
              CheckService("smtpsvc");

                          
              MailMessage mailMsg = new MailMessage(FromEmailToEmail);
                          
              mailMsg.Subject Subject.Trim.ToString;
                          if (
              ToCC.Count != 0mailMsg.CC.Add(ToCC.ToString);
                          
              mailMsg.Bcc.Add(ToBCC.ToString);
                          
              mailMsg.IsBodyHtml IsHTML;
                          
              mailMsg.BodyEncoding Encoding.UTF8;
                          
              mailMsg.SubjectEncoding mailMsg.BodyEncoding;
                          
              mailMsg.Body Body.Trim.ToString;
                          if (
              Attachments != null)
                          {
                              
              Attachment mailAttachment;
                              foreach (
              Attachment mailAttachment in Attachments)
                                  
              mailMsg.Attachments.Add(mailAttachment);
                          }

                          
              SmtpClient client = new SmtpClient(SMTP.Trim.ToString);
                          try
                          {
                              
              client.Host "localhost";
                              
              client.Send(mailMsg);
                          }
                          catch (
              Exception ex) { throw ex; }
                          return 
              true;
                      }

                      public 
              void CheckService(string ServiceName)
                      {
                          
              //Ensure the SMTP Service is installed.
                          //Loop through all the services on the machine and find the SMTP Service.
                          
              ServiceController[] services ServiceController.GetServices;
                          
              ServiceController service null;
                          
              bool blnHasSmtpService false;
                          
              string Message "";

                          foreach (
              ServiceController service in services)
                          {
                              if (
              service.ServiceName.ToLower != ServiceName.ToLower)
                                  continue;
                              
              blnHasSmtpService true;
                              break;
                          }
                          if (!
              blnHasSmtpServiceMessage += "- There're no SMTP service on your system." Environment.NewLine;

                          if (
              service.Status != ServiceControllerStatus.Running)
                          {
                              try
                              {
                                  
              service.Start();
                              }
                              catch (
              Exception ex) { throw ex; }
                          }
                      }
                  }
              }  
              • To send an e-mail should be the SMTP service status and operated on your computer and be connected to the Internet and then can be used SendEmail () to send.

              Know the screen resolution

                string ScrRes Screen.PrimaryScreen.Bounds.Width "X" Screen.PrimaryScreen.Bounds.Height;