advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Rate this item | 0 users have rated this item.
Email this articleEmail this article
 
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.

  Next Page: SQL Server Compact 3.5 Database
Page 1: IntroductionPage 3: Creating a SQL Compact Edition Database Programmatically
Page 2: SQL Server Compact 3.5 DatabasePage 4: XML
Untitled
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES