, ReadDecimal(), ReadDouble(), ReadInt16(), ReadInt32(), ReadInt64(), ReadSByte(), ReadSingle() , ReadString(), ReadUInt16(), ReadUInt32(), and ReadUInt64().

Creating a FileExplorer

Now that you have seen how to use the various classes to manipulate files and directories, let's put them to good use by building a simple file explorer that displays all the subdirectories and files within a specified directory.

The following program contains the PrintFoldersinCurrentDirectory() function, which recursively traverses a directory's subdirectories and prints out its contents:

class Program {

 static string path = @'C:Program FilesMicrosoft Visual Studio 9.0VC#';

 static void Main(string[] args) {

  DirectoryInfo di = new DirectoryInfo(path);

  Console.WriteLine(di.FullName);

  PrintFoldersinCurrentDirectory(di, -1);

  Console.ReadLine();

 }

 private static void PrintFoldersinCurrentDirectory(

  DirectoryInfo directory, int level) {

  level++;

  //---print all the subdirectories in the current directory---

  foreach (DirectoryInfo subDir in directory.GetDirectories()) {

   for (int i = 0; i <= level * 3; i++)

   Console.Write(' ');

   Console.Write('| ');

   //---display subdirectory name---

   Console.WriteLine(subDir.Name);

   //---display all the files in the subdirectory---

   FileInfo[] files = subDir.GetFiles();

   foreach (FileInfo file in files) {

    //---display the spaces---

    for (int i = 0; i <= (level+1) * 3; i++) Console.Write(' ');

    //---display filename---

    Console.WriteLine('* ' + file.Name);

   }

   //---explore its subdirectories recursively---

   PrintFoldersinCurrentDirectory(subDir, level);

  }

 }

}

Figure 11-2 shows the output of the program.

Figure 11-2

The Stream Class

A stream is an abstraction of a sequence of bytes. The bytes may come from a file, a TCP/IP socket, or memory. In .NET, a stream is represented, aptly, by the Stream class. The Stream class provides a generic view of a sequence of bytes.

The Stream class forms the base class of all other streams, and it is also implemented by the following classes:

□ BufferedStream — Provides a buffering layer on another stream to improve performance

□ FileStream — Provides a way to read and write files

□ MemoryStream — Provides a stream using memory as the backing store

□ NetworkStream — Provides a way to access data on the network

□ CryptoStream — Provides a way to supply data for cryptographic transformation

□ Streams fundamentally involve the following operations:

 □ Reading

 □ Writing

 □ Seeking

The Stream class is defined in the System.IO namespace. Remember to import that namespace when using the class.

The following code copies the content of one binary file and writes it into another using the Stream class:

try {

 const int BUFFER_SIZE = 8192;

 byte[] buffer = new byte[BUFFER_SIZE];

 int bytesRead;

 string filePath = @'C: empVS2008Pro.png';

 string filePath_backup = @'C: empVS2008Pro_bak.png';

 Stream s_in = File.OpenRead(filePath);

 Stream s_out = File.OpenWrite(filePath_backup);

 while ((bytesRead = s_in.Read(buffer, 0, BUFFER_SIZE)) > 0) {

  s_out.Write(buffer, 0, bytesRead);

 }

 s_in.Close();

 s_out.Close();

} catch (Exception ex) {

 Console.WriteLine(ex.ToString());

}

This first opens a file for reading using the static OpenRead() method from the File class. In addition, it opens a file for writing using the static OpenWrite() method. Both methods return a FileStream object.

While the OpenRead() and OpenWrite() methods return a

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату