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(0, 10); 
        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 FromEmail, MailAddress ToEmail, string Subject, MailAddressCollection ToCC, MailAddressCollection ToBCC, string Body, string SMTP, bool IsHTML, ArrayList Attachments)
                      {
                          
              CheckService("smtpsvc");

                          
              MailMessage mailMsg = new MailMessage(FromEmail, ToEmail);
                          
              mailMsg.Subject = Subject.Trim.ToString;
                          if (
              ToCC.Count != 0) mailMsg.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 (!
              blnHasSmtpService) Message += "- 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;

                  1 comment: