Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Writing evaluation version code with C#
By Waheed Khan

A while back I was asked to write a code so it can be used to expire an application after 30 days (evaluation version). I worte this code in VB then i decided to write it with C#. It is very easy to work with dates in .NET. Here my intention is to show how to work with dates, not to apply copyright protection to the software.

In this code I have three input properties. "daysSelect" property is set to pass the number of days that you wants to expire your application, "dateInstall" property is set to pass the installation date which can be saved during the installation in a registry, file or in a dll, and "evaluationVersion" property is set to "ON". I know this process may be not safe proof, again my intention is to show you how to work with dates not to apply copyright protection to the software.

expire.cs

using System;


namespace expireApp 
{
  public class expire
  {   
    private double _daysSelect;
    private string _dateInstall;
    private TimeSpan _daysRemaining;
    private string errors = "NONE";
    private string _evaluationVersion = "OFF";        
    private DateTime dateInstalled;
    private DateTime dateEnd;    
    private bool status;
          
    public double daysSelect 
    { 
      set 
      { 
        _daysSelect = value; 
      } 
    }

    public string dateInstall 
    { 
      set 
      { 
        _dateInstall = value; 
      }                    
    }
                 
    public bool expireStatus
    {
      get 
      {   
        compareDate();
        return(status);  
      }  
    }

    public string errorStatus
    {
      get
      {
        errorCheck();
        return(errors);
      }
    }
         
    public double daysRemaining
    {
      get
      {
        _daysRemaining = dateEnd.Subtract(getCurrentDate());  
        return(_daysRemaining.TotalDays + 1);
      }
    }
    
    private void errorCheck()
    {
      if (_evaluationVersion != "ON" && _evaluationVersion != "OFF" && _evaluationVersion == "")
      {
        errors = "_evaluationVersion Tag is not properly set!!!";
      }
      if (_evaluationVersion == "OFF")
      {
        errors = "_evaluationVersion Tag property is set to OFF";
      }
      if (_daysSelect == 0)
      {
        errors = "Expiration days cannot be zero or empty !!!";
      }
      if (_dateInstall == "")
      {
        errors = "Installation date is empty or null value !!!";
      }

    }
    
    private void start()
    {
      if (_evaluationVersion == "ON")
      {        
        if (_daysSelect != 0 && _dateInstall != "" )
        {
          parseTheDate(_dateInstall);
          getDateEnd();          
        } 
        else 
        {  
          errorCheck();  
        }
      }
      else if (_evaluationVersion == "OFF") 
      {
        errorCheck();
      }      
                        
    }
    
    public string evaluationVersion 
    { 
      set 
      { 
        _evaluationVersion = value;
        start(); 
      } 
    }
    
    private void parseTheDate(string localdate)
    {
      try 
      {
        dateInstalled = DateTime.Parse(localdate);       
      }
      catch(Exception e)
      {
        errors = e.ToString();
      }
    }    
         
    // retun the current date  
    private DateTime getCurrentDate()
    {
      return(DateTime.Today);
    }
         
    // compare the dates
    private void compareDate() 
    {    

      DateTime dateExpire;
      DateTime dateCurrent;
      bool checkStatus = false;
   
      dateExpire = dateEnd;
      dateCurrent = getCurrentDate();
     
      if (dateCurrent <= dateExpire) 
      {
        checkStatus = true;
      } 
 
      if (dateCurrent < dateInstalled) 
      {
        checkStatus = false;
      }
      
      if (dateCurrent > dateExpire) 
      {
        checkStatus = false;
      }
       
      status = checkStatus;
    }
      
    // find the last date      
    private void getDateEnd()
    {
      double i = 0;
      DateTime dateLast;
             
      dateLast = dateInstalled.AddDays(i);
             
      while (i < _daysSelect) 
      {
        dateLast = dateInstalled.AddDays(i);
        i += 1;
      }                   
         
      dateEnd = dateLast;
    }
        
  } // class expire ends here


  class Test
  { 
       static void Main()
       {       
             expire exp = new expire();
             
            exp.daysSelect = 30;           
             exp.dateInstall = "05/13/2002"; 
             exp.evaluationVersion = "ON";
               
             bool status = false;
             status = exp.expireStatus;
         
             if (status == true)
             {
               Console.WriteLine(exp.daysRemaining + " day(s) remaining " );
             }
             else if (status == false)
             {
               Console.WriteLine("Evaluation copy is expired!!!");        
             }
                          
             Console.WriteLine("Error : " + exp.errorStatus);
             
       }

  }//Test class
  
}//namespace expireApp