logo search
CSharp_Prog_Guide

Копирование каталогов

В этом примере демонстрируется использование классов ввода-вывода для копирования каталога с одного места на другое. В этом примере пользователь может выбирать, следует ли также копировать подкаталоги. Если подкаталоги копируются, то в этом примере метод рекурсивно копирует их путем вызова самого себя для каждого последующего подкаталога до тех пор, пока не останется ни одного каталога для копирования.

Пример

---------

How to: Open and Append to a Log File

StreamWriter and StreamReader write characters to and read characters from streams. The following code example opens the log.txt file for input, or creates the file if it does not already exist, and appends information to the end of the file. The contents of the file are then written to standard output for display. As an alternative to this example, the information could be stored as a single string or string array, and the WriteAllText or WriteAllLines method could be used to achieve the same functionality.

Example

using System;

using System.IO;

class DirAppend

{

public static void Main(String[] args)

{

using (StreamWriter w = File.AppendText("log.txt"))

{

Log ("Test1", w);

Log ("Test2", w);

// Close the writer and underlying file.

w.Close();

}

// Open and read the file.

using (StreamReader r = File.OpenText("log.txt"))

{

DumpLog (r);

}

}

public static void Log (String logMessage, TextWriter w)

{

w.Write("\r\nLog Entry : ");

w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),

DateTime.Now.ToLongDateString());

w.WriteLine(" :");

w.WriteLine(" :{0}", logMessage);

w.WriteLine ("-------------------------------");

// Update the underlying file.

w.Flush();

}

public static void DumpLog (StreamReader r)

{

// While not at the end of the file, read and write lines.

String line;

while ((line=r.ReadLine())!=null)

{

Console.WriteLine(line);

}

r.Close();

}

}