Monday, May 31, 2010

Thread Safe Singleton Pattern

namespace Organization
{
    class Employee
    {
        // Cretae a static object of the class   
       private static Employee _employee = null;
       private static object padlock = new object();
       
        // This method will  be called whenever an instance of the class will be needed
        public static Employee GetInstance()
        {
     lock(padlock)
     {
            if (_employee == null)
            {
                _employee = new Employee();
            }
     }
     return _employee
}
      
        // Default constructor is specified as private, so that it coldnot be used from outside
        private Employee()
        {
        }
    }
}

Singleton Design Pattern

namespace Organization
{
    class Employee
    {
        // Cretae a static object of the class 
        private static Employee _employee = null;
     
       // This method will  be called, whenever an instance of the class will be needed
        public static Employee GetInstance()
        {
            if (_employee == null)
            {
                _employee = new Employee();
            }
            return _employee;
        }

       // Default constructor is specified as private so that it could not be used from outside
        private Employee()
        {
        }
    }
}

Thursday, May 27, 2010

Saving User Preferences in C#.net Application

Preview:
In some applications we want to store the user preferences, like size and location, of the .net application. Not even store the user preference we required to give some option to user to change his preferences.

Where to store:
There are many options to achieve this. Either we can use XML file to store these settings in and can read these settings while launching of the application or we can use settings file that is inbuilt XML file to store user settings.

XML is a better option if we want control those options at the customer end without launch of the application. Also XML can be stored on any custom path either opted by developer or opted by user to store things.

Settings file is can't be customized, once it is deployed on user's machine, neither by user nor by developer. It is also an XML file but inbuilt in project. Settings store in this file support a vast range of data types. We are using here string and Boolean type only.

Settings File:
Settings file is there in the project itself. You can open the settings file either from the Properties/Settings.settings or you can open it from project properties and settings tab. It has a very friendly UI. We can add the settings easily into this settings file. Figure 1.1 shows the settings file from the Properties folder and Fig 1.2 shows the settings file opened from project>>properties>>settings.













Fig 1.1 Properties/Settings.settings














Fig 1.2  project properties and settings tab


How to load settings:
Here is the method that can be written while loading of the application to set the location ans the start position of it. This is a well documented and self explanatory method. We have handled the FirstLaunch of the application using an Boolean variable "FirstLaunch ". If application is launched first time then set thelocation to center of screen.



















Fig 1.3  Load settings

How to update settings: 
When user has finished the changed in the application or when he is closing the application we can update the settings using following method. Saving all the settings (Size and Location here) and set the FirstLaunch to false.















Conclusion:
Here we have seen that it is very easy to use the Settings file in the .net application. How we can accommodate the settings file in the project for reading and for updating the user preferences.

Tuesday, May 25, 2010

The process account used to run ASP.NET must have read access to the IIS metabase

If you get following error while deploying the WCF service:

Error: The process account used to run ASP.NET must have read access to the IIS metabase (e.g. IIS://servername/W3SVC).
Follow following command to overcome this problem:

Monday, May 10, 2010

Copy files and folder from source location to destination location

Copy files/folders from old folder to new Folder recursively.
private static void MoveAll(string pobjSource, string pobjTarget)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(pobjTarget) == false)
{
Directory.CreateDirectory(pobjTarget);
}
foreach (string filename in Directory.GetFiles(pobjSource, "*.*"))
{
File.Copy(filename, Path.Combine(pobjTarget, Path.GetFileName(filename)));
}
// Copy each subdirectory using recursion.
foreach (string objSourceSubDir in Directory.GetDirectories(pobjSource))
{
MoveAll(objSourceSubDir, Path.Combine(pobjTarget, Path.GetFileName(objSourceSubDir)));
}
}