Showing posts with label c#. Show all posts
Showing posts with label c#. 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, February 3, 2011

How to make program to get device drivers

Where do we find device drivers??
We can we find device drivers in the registry editor and in the following key:
HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Class



Where this key (Class) contains many of the subkeys that represent device drivers, as we see in the picture that the names of these keys is a (GUID)
Under these subkeys we find other sub-keys have their names, like this: 0000-0001-0002 - etc.

In these keys we find the required information, as we see there are values in these keys contain information on device drivers, most important of these values are:

  1. DriverDesc: which represents the name of the driver
  2. DriverVersion: which is the version of the driver
  3. DriverDate: which is the date the driver
  4. InfPath: which represents the name of the Inf file for this this driver, which is located in the folder: C: \ Windows \ Inf
  5. ProviderName: which represents the name of the manufacturer of this driver, this is very important since by this value we can identify the drives that install automatically with the system and its value is 'Microsoft', either to have installed by the user will be value certainly not a 'Microsoft', but 'Intel' Or 'Relatek' for example

The program interface:
The program interface consists of ListView to display the drives and their information, as well as some buttons such as: Get Drivers List and Backup Drivers.


How we will make a backup??
We can do it by the Inf file for each driver (*. Inf)Where contains inside a section containing the names of the files that make up the driver.
Our task is to extract the names of the files from the Inf file and then copy it to the location specified by the user.
Conclusion:
This lesson was just a paving and a brief explanation for the work through the program in general .. It sounds easy for a moment, but its implementation will be a little harder, for reasons I will mention in other parts of this lesson, but surely there is nothing difficult or impossible in the world of programming.
Author: Abdullah Alezzo

Monday, January 31, 2011

GDI+ Introducation - C#

What is GDI+ :

This technique is the upgraded version of the GDI are used in the drawing for the making new tools, or for any purpose that needs to draw such a chart ...

Principles in the GDI +:
Create a new project Windows Forms Application, and then place a button on the form and type in the event Click:
Graphics myg = this.CreateGraphics();
Pen myp = new Pen(Brushes.Red, 2);
myg.DrawLine(myp, 0, 0, this.Width, this.Height);
myg.Dispose();
Try the code ...
The code will draw a red line of the upper-right corner of the form to the lower right corner ...

 

Friday, January 28, 2011

Hanoi Towers - C#

Here I'll show you the issue of Towers of Hanoi code .. Using a recursive way:

namespace HanoiTowers
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;

            do{
                Console.WriteLine("Enter number: ");
                n = Int32.Parse(Console.ReadLine());
            }while(n<=0);

            Hanoi(n, 'A', 'B', 'C');
            Console.ReadLine();
        }

        static void Hanoi(int n, char A, char B, char C)
        {
            if (n == 1)
                Console.WriteLine("{0} --> {1}", A, C);
            else
            {
                Hanoi(n - 1, A, C, B);
                Console.WriteLine("{0} --> {1}", A, C);
                Hanoi(n - 1, B, A, C);
            }
        }
    }
}

This picture shows the method of work on this issue:

Hanoi Towers

Monday, January 24, 2011

Handling strings in C#

We can deal with strings as an array of characters. This means that we can access the characters of the string separately using a formula as follows:
string m = "a world";
char c = m[1];
Reading the characters in this way is possible, but we can not use this method to assign characterss to any text string that is acceptable to code the following:
m[1] = 'w';
So that we can get a char array can write to it we use it ToCharArray () which returns a array characters represent the array codes the text string:
string m = "a world";
char[] c = m.ToCharArray();

Structures in C#

Structures:

The second kind of the types of variables is the structure 'Struct' acronym for the word (structure)
Represent the structure of different parts of the data can be different for each part type. It enables us to define our species of special variables, depending on the structure ...
For example:
Suppose we wish to save the path for the site specific from the point where this path composed of direction and distance measured in miles and simplify things we'll impose that the trend is one of the four directions of knowledge within a census orientation
And that the mileage represented by type double
Now we can use separate converts to represent this path as in the following code:


orientation direction;
double distance;

There is nothing wrong in what we have done but it would be simpler if we used a single variable to store this information

Top 10 reasons C# is better than VB.NET

Let’s get practical, religion aside, with an eye on programmer productivity, here are the top 10 reasons why C# is better than VB.NET, in no particular order. If you have some other ones, let me know.

1. In C# the line ends when I type the semicolon. The practical offshoot of this is that I write code like this
string sql = @"SELECT *
              FROM SomeSuchTable
              WHERE ID=’WhatHaveYou’
              ORDER BY ThisAndThatField ASC ";

So when someone hands you a SQL statement you can just paste it into your code and the output (i.e. carriage returns) will still keep the format. In VB.NET, you would have to write the following to keep the formatting.

Using Objects Instead of Enumerations

Introduction

Enumerations are one of many useful tools at the fingertips of developers. Most of us have been using them for years. Because they are so common and easy to use, I believe we sometimes overuse them. An enumeration at its core is merely a number value. Try this in C# and you will see how easy it is to convert between enumerations and integer values. Enumerations are useful because they are named collections of values which are associated under the surface with integer values.
This example shows how one could quickly and easily print out the names and number values of an enumeration.

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;