Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

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 Files\Microsoft Visual Studio 9.0\VC#";

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 112 The Stream Class A stream is an abstraction of a sequence of - фото 165

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 Streamclass. TheStream class provides a generic view of a sequence of bytes.

The Streamclass 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 Streamclass is defined in the System.IOnamespace. 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 Streamclass:

try {

const int BUFFER_SIZE = 8192;

byte[] buffer = new byte[BUFFER_SIZE];

int bytesRead;

string filePath = @"C:\temp\VS2008Pro.png";

string filePath_backup = @"C:\temp\VS2008Pro_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 FileStreamobject.

While the OpenRead()and OpenWrite()methods return a FileStreamobject, you can actually assign the returning type to a Streamobject because the FileStreamobject inherits from the Streamobject.

To copy the content of one file into another, you use the Read()method from the Streamclass and read the content from the file into an byte array. Read()returns the number of bytes read from the stream (in this case the file) and returns 0 if there are no more bytes to read. The Write()method of the Streamclass writes the data stored in the byte array into the stream (which in this case is another file). Finally, you close both the Streamobjects.

In addition to the Read()and Write()methods, the Streamobject supports the following methods:

ReadByte()— Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream

WriteByte()— Writes a byte to the current position in the stream and advances the position within the stream by 1 byte

Seek()— Sets the position within the current stream

The following example writes some text to a text file, closes the file, reopens the file, seeks to the fourth position in the file, and reads the next six bytes:

try {

const int BUFFER_SIZE = 8192;

string text = "The Stream class is defined in the System.IO namespace.";

byte[] data = ASCIIEncoding.ASCII.GetBytes(text);

byte[] buffer = new byte[BUFFER_SIZE];

string filePath = @"C:\temp\textfile.txt";

//---writes some text to file---

Stream s_out = File.OpenWrite(filePath);

s_out.Write(data, 0, data.Length);

s_out.Close();

//---opens the file for reading---

Stream s_in = File.OpenRead(filePath);

//---seek to the fourth position---

s_in.Seek(4, SeekOrigin.Begin);

//---read the next 6 bytes---

int bytesRead = s_in.Read(buffer, 0, 6);

Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer, 0, bytesRead));

s_in.Close();

s_out.Close();

} catch (Exception ex) {

Console.WriteLine(ex.ToString());

}

BufferedStream

To improve its performance, the BufferedStreamclass works with another Streamobject. For instance, the previous example used a buffer size of 8192 bytes when reading from a text file. However, that size might not be the ideal size to yield the optimum performance from your computer. You can use the BufferedStreamclass to let the operating system determine the optimum buffer size for you. While you can still specify the buffer size to fill up your buffer when reading data, your buffer will now be filled by the BufferedStreamclass instead of directly from the stream (which in the example is from a file). The BufferedStreamclass fills up its internal memory store in the size that it determines is the most efficient.

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x