Tuesday, March 26, 2013

Creating Unique Application ID for Silverlight Applications


In one of my projects, it became necessary to run a portion of the code only in my computer. This was a Silverlight Application and there were many developers working on this. This section of the code was unique to my computer alone and we need to execute this code only from my computer. After digging around for an answer, finally we came across this solution.
Silverlight provides an ability to store data in “Isolated Storage”. Using this technique we store a unique key in my computer (source code shown below). First time, we examine this key in ‘Debug Mode’ and then copy and hard code this. In the main calling function we check for this ‘Unique Key’. If it matches then we execute the special code pertaining to my computer alone.
Hope this helps someone.

Cheers
Anand
========== Calling Code Begins ==================

If (GetUniqueAppID() == “Hardcoded Unique Key Code”)
{

// Execute this special portion of this code unique for my machine and this application

}
========== Calling Code Ends ==================

========== Unique Key Generating Code ===========

        private string GetUniqueAppID()
        {
            string keyname = "UniqueAppID";
            string key = "";
            try
            {
                if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains(keyname))
                    key = (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[keyname] ?? "").ToString();
                if (string.IsNullOrEmpty(key))
                {
                    key = Guid.NewGuid().ToString();
                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[keyname] = key;
                }
            }
            catch
            {
                key = "";
            }
            return key;
        }


========== Unique Key Generating Code ===========

 

 

No comments:

Post a Comment

All Blogs so far ...