You are on page 1of 6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

Week 13: File IO and Isolated Storage


Topics Concept review:
Working with File System Understanding the Isolated Storage Namaspace System.IO is the region of the base class libraries devoted to file-based (and memory-based) input and output (I/O) services. Working with File System: Classes Directory and File Classes DirectoryInfo, FileInfo and Drive Info Isolated storage API provide a safe sandbox where applications can read/write data, regardless of which code group they are placed into and without the need to alter the default security policies. Use using System.IO;
using System.IO.IsolatedStorage;

1/6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

PART I: WORKING WITH FILE SYSTEM


Exercise 1:
1. The FileSystemInfo class provides which services? a. Only basic information about the file system such as the amount of free space on disk b. Sophisticated operations such as the ability to access network drives c. Operations common to both files and directories 2. The DirectoryInfo and Directory classes provide almost identical functionality. Similarly, the FileInfo and File classes provide identical functionality. Why might clients choose to use the Directory or File classes? a. They have static methods which may offer more convenient usage syntax b. They bypass all security checks to allow the program to do anything it needs to do c. They run inside their own version of the CLR thus increasing performance 3. True or false: it is possible to use a StreamWriter to write numeric types such as int and double to a text file a. True b. False 4. True or false: it is possible to write the simple types directly to a binary file using the methods in the FileStream class a. True b. False What is your answer (chose a,b, or c):
1 2 3 4

Exercise 2:
using System; using System.IO; public class UsingDirectory {

2/6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

static void FunWithDirectoryType() { string[] drives = Directory.GetLogicalDrives(); Console.WriteLine("Here are your drives:"); foreach (string s in drives) Console.WriteLine("--> {0} ", s); } static void Main(String[] args) { FunWithDirectoryType(); Console.Read(); } }

2.1. What is written by this program?

Exercise 3:
using System; class Program { static void Main(string[] args) { Console.WriteLine("***** Simple IO with the File Type *****\n"); string[] myTasks = {"Fix bathroom sink", "Call Dave", "Call Mom and Dad", "Play Xbox 360"}; File.WriteAllLines(@"C:\tasks.txt", myTasks); foreach (string task in File.ReadAllLines(@"C:\tasks.txt")) { Console.WriteLine("TODO: {0}", task); }

3/6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

Console.ReadLine(); } }

3.1. From this program, when compiled, there are some errors, why?

3.2. Correct it, what is written by this program?

4/6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

PART II: UNDERSTANDING THE ISOLATED STORAGE


Exercise 1:
using System; using System.IO; using System.IO.IsolatedStorage; class Program { static void WriteTextToIsoStorage() { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly()) { using (IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("MyData.txt", FileMode.OpenOrCreate, store)) { using (StreamWriter sw = new StreamWriter(isStream)) { sw.WriteLine("This is my data."); } } } } static void ReadTextFromIsoStorage() { using (IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("MyData.txt", FileMode.Open, FileAccess.Read, store)) { using (StreamReader sr = new StreamReader(isStream)) { string allTheData = sr.ReadToEnd(); Console.WriteLine(allTheData); } } } static void Main(String[] args) { WriteTextToIsoStorage(); ReadTextFromIsoStorage(); Console.Read(); } } 5/6

HaNoi University of Technology

Week 13: File IO and Isolated Storage Mastering C# 2008

1.1. When compiled, this program cannot run, why?

1.2. Correct it and explain its output.

6/6

You might also like