Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Persisting Data in Your Windows Mobile Device

Learn about the three methods of persisting data and which is the right one for your device.


advertisement
n a previous article on Windows Mobile, I showed how you can develop a simple Windows Mobile 6 application and then deploy it by creating a CAB file. I also showed you the various connectivity options available in Windows Mobile that allow you to connect to the outside world. At some point in time, you'll also need the ability to store data onto the device persistently—things like saving preferences, user-specific data, configuration information, and so on. You have a number of methods at your disposal to persist data. This article will explain your options and help you choose the right one for your device.

Files
The most common way to persist data to storage is to use a file. Files allow you to write text or binary content to them and then easily read said content. To manipulate files, you can use the File class, which provides static methods for dealing with files. The File class is defined within the System.IO namespace, so before using the File class, you need to import the System.IO namespace:

using System.IO;

The following example shows how you can open a file for writing text content using the AppendText() method of the File class:


string Path = @"textfile.txt"; string str1 = "This is a string."; string str2 = "This is another string."; StreamWriter sw = File.AppendText(Path); sw.Write(str1); sw.WriteLine(str2); sw.Close();

The AppendText() method returns a StreamWriter object (also defined in the System.IO namespace), which allows you to write characters to a stream. The StreamWriter class has two methods you can use to write text into the file: Write() or WriteLine().

To open a text file for reading, use the File class' OpenText() method:

StreamReader sr = File.OpenText(Path); string strRead; while ((strRead = sr.ReadLine()) != null) { MessageBox.Show(strRead); }

The OpenText() method returns a StreamReader object (also defined in the System.IO namespace), which allows you to read characters from a stream. In the above example, you read each individual line from the file and print them out using the MessageBox class until there are no more lines to be read.

To deal with binary contents, you should use the File class' OpenRead() method to open a file for reading and the OpenWrite() method to open it for writing. Both methods return a FileStream object (also defined in the System.IO namespace). You can then use the BinaryReader class to read binary data from the FileStream object, and the BinaryWriter class to write binary data to a FileStream object. Listing 1 shows how to copy an image (a binary file) byte-by-byte into another file, essentially making a copy of the file.

While writing to and reading from files is a straightforward affair, the downside is that there is no sophisticated mechanism to help you manage the content of the file. For example, suppose you want to replace part of the file with some data. You'd need to seek to the exact location of the data before you could replace it. And in most cases, this simple task involves reading from the original file, filtering the necessary data, and then rewriting the data back to the original file. Hence, you should use files for storing simple data—comments, error logs, and so on.

For more structured data, using a database is more appropriate.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap