My First Flash Ecard Flash vs Silverlight: Simple Game System
Dec 01

Sometimes, your application may need to save the user state or progress for various purposes. Rather than storing the information to server, both technology provided a very convenient way to store some temporary data into user computer.

However, the storage has it own limit size for security reason.

Comparison

Flash implementation: 30 minutes
Silverlight implementation: 2 hours (Implemented First)
What’s the difference?

  • flash.net.SharedObject [AS3] vs System.IO.IsolatedStorage.IsolatedStorageFile [C#]

Default local storage limit

  • Flash: 100 KB (Can be adjusted to unlimited by user)
  • Silverlight: 1024 KB

Source codes

Flash

Silverlight

flash.net.SharedObject [AS3] vs System.IO.IsolatedStorage.IsolatedStorageFile [C#]

Using Local Storage is pretty easy in Flash. It’s because you only have to deal with the shared data as if local Object.

Please note that you have to call the flush() method after updating the object. Just to make sure it is saved into file system.

// AS3
// get the shared object
var so: SharedObject = SharedObject.getLocal(OBJECT_NAME);

// load data from local storage
if(so.data.text){
	TextArea.text = so.data.text;
}

// save data to local storage
so.data.text = "New Text";
so.data.flush();

In Silverlight, the local storage is actually a predefined path of storage directory. You can read and save data as if reading and writing files.

// C#
// load data from local storage
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if(store.FileExists(FILENAME)){
	isfs = new IsolatedStorageFileStream(FILENAME, FileMode.OpenOrCreate, store);
	StreamReader streamReader = new StreamReader(isfs);
        string s;
	while ((s = streamReader.ReadLine()) != null)
		TextArea.Text += (s + '\n');
	streamReader.Close();
}

// save data to local storage
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(FILENAME, FileMode.OpenOrCreate, store);
StreamWriter streamWriter = new StreamWriter(isfs);
streamWriter.Write("New Text");
streamWriter.Flush();
streamWriter.Close();

Shares and Enjoy~

Did you like this post?

Subscribe here:  

17 Responses to “Silverlight vs Flash: Local Storage”

  1. Justin Van Patten Says:

    Silverlight has an IsolatedStorageSettings class that is similar to Flash’s SharedObject. It can be used to easily store objects in isolated storage without having to use the lower-level file-based APIs. Using it would look like this:

    var settings = IsolatedStorageSettings.ApplicationSettings;

    // load data from local storage
    string text;
    if (settings.TryGetValue(OBJECT_NAME, out text)) {
    TextArea.Text = text;
    }

    // save data to local storage
    settings[OBJECT_NAME] = “New Text”;
    settings.Save();

    Hope this helps!

    Regards,

    Justin Van Patten
    Program Manager
    Microsoft

  2. admin Says:

    Thanks, that helps.

    Acutally, there aren’t much reference on IsolatedStorage and I used a stupid method.
    Thanks anyway.

  3. Silverlight vs Flash: Lokale Speicherung at Programming with Silverlight, WPF & .NET Says:

    [...] mal zeigt der Vergleich zwischen Silverlight und Flash die lokale Speicherung. Auffallend dabei ist das lokale [...]

  4. Gaku Says:

    I can’t seem to paste a large chunk of text into the Silverlight version, is that a bug with the code you’ve written? It seems to work ok with the Flash version. I voted for the Flash version because of that but I’m not sure what caused the problem.

  5. Justin Van Patten Says:

    Hi Terence. Don’t worry about it, you’re method still gets the job done. You can find out more about isolated storage here: http://msdn.microsoft.com/en-us/library/bdts8hk0(vs.95).aspx

    I should also mention that if you want to use the lower-level isolated storage APIs instead of IsolatedStorageSettings (as you do above), you can simplify the code a bit as follows:

    // C#
    // load data from local storage
    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
    if (store.FileExists(FILENAME)) {
    using (var stream = store.OpenFile(FILENAME, FileMode.Open))
    using (var reader = new StreamReader(stream)) {
    TextArea.Text = reader.ReadToEnd();
    }
    }
    }

    // save data to local storage
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var stream = store.OpenFile(FILENAME, FileMode.OpenOrCreate))
    using (var writer = new StreamWriter(stream)) {
    writer.Write(”New Text”);
    }

    Cheers,

    Justin Van Patten
    Program Manager
    Microsoft

  6. admin Says:

    How large is the chunk of text you placed?

    I have placed about 100 lines of text and it is working quite well for me.

  7. maximelamure Says:

    Hi,
    The default size limit with IsolatedStorage is 1MO. But, if your reach the limit, Silverlight prompts you to increase the size.

  8. Steve Buchok Says:

    For Silverlight.

    In addition to maximelamure response. You can programmatically set the size for the IsolatedStorage on a users computer but must be done through a specific event like a button click (can’t do it onload of the app), this will then propmt the user to accept the change in size for the Isolated storage. This means that if you have a large business app that needs to save alot of info to IS you can just have the size increased to what you think the max will be.

  9. Roland Says:

    Silverlight version throws exception for me.

  10. Huson Says:

    Your Silverlight application always prompts me to install Silverlight in Firefox (3.0.4), but actually I have installed the Silverlight Plugin (2.0.31005.0).

  11. admin Says:

    Huson, thx for reporting the problem. However, I think I have to report this to Microsoft as well.

  12. Gaku Says:

    It was all the text from a random page I had open in another tab. I went back and checked, it’s just under 5,000 words.

  13. 梦云 » 来一个邪恶的RP测试工具(下) Says:

    [...] Silverlight vs Flash: Local Storage [...]

  14. alan Says:

    AcceptsReturn=”True” should be set on the silverlight text box to allow multi line mode. this is probably why you can’t paste in a large block of text.

  15. Quentin T, Pierce Says:

    I started out with audio problems with flash that have continued thru test of silverlight / I’ve tried everything I am ready to give up. It started a few years ago I dropped one thousand dollars on a used sony vio with what I was told would be decent software to use for audio recording and files
    which I did not find use in and the program which slips me for the moment appeared more for video and not audio however I liked the new windows xp home for about a little over a year where
    the motherboard dropped dead so I was told.
    Since then I am working with recycled throw away parts I find in the college town I live in I am fortuned to find lots of desktop throw aways that I’ve managed to get on line with but I don’t know why BUT I HAVE HAD THE WORST LUCK ENABLING AUDIO FUNCTIONS , i AM A 54 year old
    electrician and I have been around the world a few times working for years overseas on high voltage installations remodeling and new construction of power plants in micronesia on many different islands. Yet I feel like I have put more effort in attempting to make the audio system
    work. I play guitar and keyboards and I’ve to enable my synthsiser up since becomming aware
    of the system but I’d settle for just to hear the audio on flash or any other application
    I want to say again I have gone through troubleshooting procedures and a thousand other back and forth actions until I am blue in the face and sadly I am not exaggerating

  16. Quentin T, Pierce Says:

    in conj. it could be faulty equipment one action was every time I’d enter a sound scheme it’d
    disappear in a faded appearance when I’d restart to see if events improved.

  17. admin Says:

    HI Quentin, anything I could help related to the topic?

    You may find out a more organized code in saving file to LocalStorage here:
    http://silverlight.shinedraw.com/#/item/4a76e8ba4b36e

Leave a Reply