{"id":425932,"date":"2024-07-10T15:00:08","date_gmt":"2024-07-10T15:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=425932"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=425932","title":{"rendered":"<span>\u041e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0432 C#<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u2014 \u043d\u0435\u043e\u0442\u044a\u0435\u043c\u043b\u0435\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432. \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043b\u043e\u0433\u043e\u0432, \u0447\u0442\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0432\u0441\u0435 \u044d\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0438\u044f \u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0432 C#.<\/p>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0432 C#<\/h2>\n<p><code>File<\/code> \u0438 <code>FileInfo<\/code> \u2014 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u0438\u043c\u0435\u043d <code>System.IO<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. \u041e\u0431\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432, \u043d\u043e \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439.<\/p>\n<p><code>File<\/code> \u2014 \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043c\u0435\u0436\u0434\u0443 \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>File<\/code>:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 string filePath = @\"C:\\example.txt\"; File.Create(filePath).Close();  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 if (File.Exists(filePath)) {     Console.WriteLine(\"\u0424\u0430\u0439\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b string content = \"Hello, World!\"; File.WriteAllText(filePath, content);  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 string readContent = File.ReadAllText(filePath); Console.WriteLine(readContent);  \/\/ \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string copyPath = @\"C:\\example_copy.txt\"; File.Copy(filePath, copyPath);  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string movePath = @\"C:\\example_moved.txt\"; File.Move(copyPath, movePath);  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 File.Delete(movePath);<\/code><\/pre>\n<p><code>FileInfo<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0444\u0430\u0439\u043b\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 FileInfo FileInfo fileInfo = new FileInfo(filePath);  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 using (FileStream fs = fileInfo.Create()) {     byte[] info = new UTF8Encoding(true).GetBytes(\"This is some text in the file.\");     fs.Write(info, 0, info.Length); }  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 if (fileInfo.Exists) {     Console.WriteLine(\"\u0424\u0430\u0439\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b using (StreamWriter writer = fileInfo.AppendText()) {     writer.WriteLine(\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442.\"); }  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 using (StreamReader reader = fileInfo.OpenText()) {     string s = \"\";     while ((s = reader.ReadLine()) != null)     {         Console.WriteLine(s);     } }  \/\/ \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string copyFileInfoPath = @\"C:\\example_copy_info.txt\"; fileInfo.CopyTo(copyFileInfoPath);  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string moveFileInfoPath = @\"C:\\example_moved_info.txt\"; fileInfo.MoveTo(moveFileInfoPath);  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 fileInfo.Delete();<\/code><\/pre>\n<p><code>FileStream<\/code> &#8212;  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>FileStream<\/code>:<\/p>\n<pre><code class=\"cs\">string fileStreamPath = @\"C:\\filestream_example.txt\";  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0444\u0430\u0439\u043b using (FileStream fs = new FileStream(fileStreamPath, FileMode.Create)) {     byte[] data = Encoding.UTF8.GetBytes(\"Hello, FileStream!\");     fs.Write(data, 0, data.Length); }  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 using (FileStream fs = new FileStream(fileStreamPath, FileMode.Open, FileAccess.Read)) {     byte[] data = new byte[fs.Length];     int numBytesToRead = (int)fs.Length;     int numBytesRead = 0;      while (numBytesToRead > 0)     {         int n = fs.Read(data, numBytesRead, numBytesToRead);          if (n == 0)             break;          numBytesRead += n;         numBytesToRead -= n;     }      string text = Encoding.UTF8.GetString(data);     Console.WriteLine(text); }  \/\/ \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c async Task WriteAsync(string path, string content) {     byte[] encodedText = Encoding.UTF8.GetBytes(content);     using (FileStream sourceStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None, 4096, true))     {         await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);     }; }  async Task&lt;string> ReadAsync(string path) {     using (FileStream sourceStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))     {         byte[] buffer = new byte[1024];         int numRead;         StringBuilder sb = new StringBuilder();         while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)         {             string text = Encoding.UTF8.GetString(buffer, 0, numRead);             sb.Append(text);         }         return sb.ToString();     } }  \/\/ \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 await WriteAsync(fileStreamPath, \"Async Hello, FileStream!\"); string asyncContent = await ReadAsync(fileStreamPath); Console.WriteLine(asyncContent);<\/code><\/pre>\n<p><code>Directory<\/code> \u0438 <code>DirectoryInfo<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439. \u041a\u0430\u043a \u0438 <code>File<\/code> \u0438 <code>FileInfo<\/code>, <code>Directory<\/code> &#8212; \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u0430 <code>DirectoryInfo<\/code> &#8212; \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>Directory<\/code>:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 string directoryPath = @\"C:\\example_dir\"; Directory.CreateDirectory(directoryPath);  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 if (Directory.Exists(directoryPath)) {     Console.WriteLine(\"\u0414\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 string[] files = Directory.GetFiles(directoryPath); foreach (string file in files) {     Console.WriteLine(file); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 string[] directories = Directory.GetDirectories(directoryPath); foreach (string dir in directories) {     Console.WriteLine(dir); }  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 Directory.Delete(directoryPath, true);<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440<code>DirectoryInfo<\/code>:<\/p>\n<pre><code class=\"cpp\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 DirectoryInfo DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 directoryInfo.Create();  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 if (directoryInfo.Exists) {     Console.WriteLine(\"\u0414\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) {     Console.WriteLine(fileInfo.Name); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories(); foreach (DirectoryInfo dirInfo in directoryInfos) {     Console.WriteLine(dirInfo.Name); }  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 directoryInfo.Delete(true);<\/code><\/pre>\n<h2>\u0427\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 FileStream  <\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FileStream<\/code> \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u043e\u0432 <code>FileMode<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f\u0430 <code>FileAccess<\/code> \u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>FileShare<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 string path = @\"C:\\example.txt\"; using (FileStream fs = new FileStream(path, FileMode.Create)) {     byte[] info = new UTF8Encoding(true).GetBytes(\"This is some text in the file.\");     fs.Write(info, 0, info.Length); }  \/\/ \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {     byte[] b = new byte[1024];     UTF8Encoding temp = new UTF8Encoding(true);     while (fs.Read(b, 0, b.Length) > 0)     {         Console.WriteLine(temp.GetString(b));     } }<\/code><\/pre>\n<p><code>FileMode.Create<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u043d, \u0435\u0441\u043b\u0438 \u043e\u043d \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442. <code>FileMode.Open<\/code> \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0444\u0430\u0439\u043b, \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>FileNotFoundException<\/code>.<\/p>\n<p>\u0427\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Read<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u0445 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<pre><code class=\"cs\">using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {     byte[] buffer = new byte[1024];     int bytesRead;     while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)     {         Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Write<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0430\u0439\u0442\u043e\u0432 \u0432 \u0444\u0430\u0439\u043b.<\/p>\n<pre><code class=\"cs\">using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) {     byte[] info = new UTF8Encoding(true).GetBytes(\"More text to add to the file.\");     fs.Write(info, 0, info.Length); }<\/code><\/pre>\n<p><strong>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0440\u0435\u0436\u0438\u043c\u0430\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/strong><\/p>\n<p><code>FileAccess<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0444\u0430\u0439\u043b\u0443: \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u0435\u043d\u0438\u0435 <code>FileAccess.Read<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u044c <code>FileAccess.Write<\/code>, \u0438\u043b\u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c <code>FileAccess.ReadWrite<\/code>.<\/p>\n<p><code>FileShare<\/code> \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043a \u0444\u0430\u0439\u043b\u0443: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>FileShare.Read<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b, \u043d\u043e \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e.<\/p>\n<pre><code class=\"cs\">\/\/ \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) {     \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445     byte[] buffer = new byte[1024];     int bytesRead = fs.Read(buffer, 0, buffer.Length);     Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));      \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445     byte[] info = new UTF8Encoding(true).GetBytes(\"Additional data\");     fs.Write(info, 0, info.Length); }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u044e\u0437\u0430\u0442\u044c <code>FileStream<\/code> \u0441 <code>FileAccess.ReadWrite<\/code> \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FileShare.Read<\/code>.<\/p>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 C#<\/h2>\n<p>\u0412 .NET <code>Stream<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0430\u0439\u0442\u043e\u0432. \u0412\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 .NET \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 <code>Stream<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f <code>FileStream<\/code>, <code>MemoryStream<\/code>, <code>NetworkStream<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <code>Stream<\/code>:<\/p>\n<ul>\n<li>\n<p><code>Read(byte[] buffer, int offset, int count)<\/code>: \u0447\u0438\u0442\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0445 \u0432 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<\/li>\n<li>\n<p><code>Write(byte[] buffer, int offset, int count)<\/code>: \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u0431\u0443\u0444\u0435\u0440\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p><code>Seek(long offset, SeekOrigin origin)<\/code>: \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p><code>Flush()<\/code>: \u043e\u0447\u0438\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0431\u0443\u0444\u0435\u0440\u044b \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>Stream<\/code>:<\/p>\n<pre><code class=\"cs\">using System; using System.IO;  class Program {     static void Main()     {         string path = \"example.txt\";          using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))         {             byte[] info = new UTF8Encoding(true).GetBytes(\"Hello, Stream!\");             fs.Write(info, 0, info.Length);         }          using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))         {             byte[] b = new byte[1024];             UTF8Encoding temp = new UTF8Encoding(true);             while (fs.Read(b, 0, b.Length) > 0)             {                 Console.WriteLine(temp.GetString(b));             }         }     } }<\/code><\/pre>\n<p><code>StreamReader<\/code> \u0438 <code>StreamWriter<\/code> \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f StreamWriter:<\/p>\n<pre><code class=\"cs\">using System; using System.IO;  class Program {     static void Main()     {         string path = \"example.txt\";          using (StreamWriter sw = new StreamWriter(path))         {             sw.WriteLine(\"Hello, StreamWriter!\");         }          using (StreamReader sr = new StreamReader(path))         {             string line;             while ((line = sr.ReadLine()) != null)             {                 Console.WriteLine(line);             }         }     } }<\/code><\/pre>\n<p><code>BinaryReader<\/code> \u0438 <code>BinaryWriter<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435. \u042d\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0445\u043e\u0440\u043e\u0448\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">using System; using System.IO;  class Program {     static void Main()     {         string path = \"example.bin\";          using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))         {             writer.Write(1.25);             writer.Write(\"Hello, BinaryWriter!\");             writer.Write(true);         }          using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open)))         {             Console.WriteLine(reader.ReadDouble());             Console.WriteLine(reader.ReadString());             Console.WriteLine(reader.ReadBoolean());         }     } }<\/code><\/pre>\n<p>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430 \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 StreamWriter:<\/p>\n<pre><code class=\"cs\">using System; using System.IO; using System.Text; using System.Threading.Tasks;  class Program {     static async Task Main()     {         string path = \"example.txt\";          using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))         {             await sw.WriteLineAsync(\"Hello, Async StreamWriter!\");         }          using (StreamReader sr = new StreamReader(path))         {             string line;             while ((line = await sr.ReadLineAsync()) != null)             {                 Console.WriteLine(line);             }         }     } }<\/code><\/pre>\n<h2>\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439<\/h2>\n<p><code>FileSystemWatcher<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c, \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u043e\u0432 \u0438 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439. <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <code>FileSystemWatcher<\/code>, \u0443\u043a\u0430\u0437\u0430\u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u0434\u043b\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430:<\/p>\n<pre><code class=\"cs\">using System; using System.IO;  public class FileSystemWatcherExample {     private FileSystemWatcher _watcher;      public FileSystemWatcherExample(string path)     {         _watcher = new FileSystemWatcher(path);         _watcher.IncludeSubdirectories = true; \/\/ \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u043f\u043e\u0434\u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430\u043c\u0438         _watcher.Filter = \"*.*\"; \/\/ \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0432\u0441\u0435\u043c\u0438 \u0444\u0430\u0439\u043b\u0430\u043c\u0438         _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |                                 NotifyFilters.Attributes | NotifyFilters.Size |                                 NotifyFilters.LastWrite | NotifyFilters.LastAccess |                                 NotifyFilters.CreationTime | NotifyFilters.Security;          \/\/ \u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f         _watcher.Created += OnCreated;         _watcher.Deleted += OnDeleted;         _watcher.Changed += OnChanged;         _watcher.Renamed += OnRenamed;         _watcher.Error += OnError;          _watcher.EnableRaisingEvents = true; \/\/ \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430     }      private void OnCreated(object sender, FileSystemEventArgs e) =>          Console.WriteLine($\"File created: {e.FullPath}\");      private void OnDeleted(object sender, FileSystemEventArgs e) =>          Console.WriteLine($\"File deleted: {e.FullPath}\");      private void OnChanged(object sender, FileSystemEventArgs e) =>          Console.WriteLine($\"File changed: {e.FullPath}\");      private void OnRenamed(object sender, RenamedEventArgs e) =>          Console.WriteLine($\"File renamed: {e.OldFullPath} to {e.FullPath}\");      private void OnError(object sender, ErrorEventArgs e) =>          PrintException(e.GetException());      private void PrintException(Exception ex)     {         if (ex != null)         {             Console.WriteLine($\"Message: {ex.Message}\");             Console.WriteLine(\"Stacktrace:\");             Console.WriteLine(ex.StackTrace);             PrintException(ex.InnerException);         }     } }<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>Filter<\/code> \u0438 <code>NotifyFilter<\/code>:<\/p>\n<ul>\n<li>\n<p><code>Filter<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0444\u0430\u0439\u043b\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 <code>*.txt<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>NotifyFilter<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0438\u043f\u044b \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432:<\/p>\n<pre><code class=\"cs\">_watcher.Filter = \"*.txt\"; _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.LastWrite;<\/code><\/pre>\n<p>\u0421\u043e\u0431\u044b\u0442\u0438\u044f <code>FileSystemWatcher<\/code> \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 <code>Created<\/code>, <code>Deleted<\/code>, <code>Changed<\/code>, <code>Renamed<\/code> \u0438 <code>Error<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u044d\u0442\u0438\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439:<\/p>\n<pre><code class=\"cs\">private void OnCreated(object sender, FileSystemEventArgs e) =>      Console.WriteLine($\"File created: {e.FullPath}\");  private void OnDeleted(object sender, FileSystemEventArgs e) =>      Console.WriteLine($\"File deleted: {e.FullPath}\");  private void OnChanged(object sender, FileSystemEventArgs e) =>      Console.WriteLine($\"File changed: {e.FullPath}\");  private void OnRenamed(object sender, RenamedEventArgs e) =>      Console.WriteLine($\"File renamed: {e.OldFullPath} to {e.FullPath}\");  private void OnError(object sender, ErrorEventArgs e) =>      PrintException(e.GetException());  private void PrintException(Exception ex) {     if (ex != null)     {         Console.WriteLine($\"Message: {ex.Message}\");         Console.WriteLine(\"Stacktrace:\");         Console.WriteLine(ex.StackTrace);         PrintException(ex.InnerException);     } } <\/code><\/pre>\n<p>\u0411\u0443\u0434\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>InternalBufferSize<\/code>, \u0435\u0441\u043b\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0431\u0443\u0444\u0435\u0440\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d 64 \u041a\u0411.<\/p>\n<hr\/>\n<p><em>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e \u043f\u0440\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c \u0438 \u043d\u044e\u0430\u043d\u0441\u0430\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 18 \u0438\u044e\u043b\u044f. <\/em><\/p>\n<p><em>\u041d\u0430 \u0443\u0440\u043e\u043a\u0435 \u043e\u0431\u0441\u0443\u0434\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0438 \u0442\u043e, \u043a\u0430\u043a \u043c\u044b \u0438\u0445 \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043b\u043e\u0432\u0438\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0431\u0449\u0438\u0435 \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u043f\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u043c\u0438 \u0432 .NET. \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e <\/em><a href=\"https:\/\/otus.pw\/4VOA\/\"><em>\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 &#171;C# Developer. Professional&#187;.<\/em><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/826776\/\"> https:\/\/habr.com\/ru\/articles\/826776\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u2014 \u043d\u0435\u043e\u0442\u044a\u0435\u043c\u043b\u0435\u043c\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432. \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043b\u043e\u0433\u043e\u0432, \u0447\u0442\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0432\u0441\u0435 \u044d\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0438\u044f \u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0432 C#.<\/p>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0432 C#<\/h2>\n<p><code>File<\/code> \u0438 <code>FileInfo<\/code> \u2014 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u0438\u043c\u0435\u043d <code>System.IO<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439. \u041e\u0431\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432, \u043d\u043e \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439.<\/p>\n<p><code>File<\/code> \u2014 \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u043c\u0435\u0436\u0434\u0443 \u0432\u044b\u0437\u043e\u0432\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>File<\/code>:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 string filePath = @\"C:\\example.txt\"; File.Create(filePath).Close();  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 if (File.Exists(filePath)) {     Console.WriteLine(\"\u0424\u0430\u0439\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b string content = \"Hello, World!\"; File.WriteAllText(filePath, content);  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 string readContent = File.ReadAllText(filePath); Console.WriteLine(readContent);  \/\/ \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string copyPath = @\"C:\\example_copy.txt\"; File.Copy(filePath, copyPath);  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string movePath = @\"C:\\example_moved.txt\"; File.Move(copyPath, movePath);  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 File.Delete(movePath);<\/code><\/pre>\n<p><code>FileInfo<\/code> \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0444\u0430\u0439\u043b\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 FileInfo FileInfo fileInfo = new FileInfo(filePath);  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 using (FileStream fs = fileInfo.Create()) {     byte[] info = new UTF8Encoding(true).GetBytes(\"This is some text in the file.\");     fs.Write(info, 0, info.Length); }  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 if (fileInfo.Exists) {     Console.WriteLine(\"\u0424\u0430\u0439\u043b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0444\u0430\u0439\u043b using (StreamWriter writer = fileInfo.AppendText()) {     writer.WriteLine(\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442.\"); }  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 using (StreamReader reader = fileInfo.OpenText()) {     string s = \"\";     while ((s = reader.ReadLine()) != null)     {         Console.WriteLine(s);     } }  \/\/ \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string copyFileInfoPath = @\"C:\\example_copy_info.txt\"; fileInfo.CopyTo(copyFileInfoPath);  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 string moveFileInfoPath = @\"C:\\example_moved_info.txt\"; fileInfo.MoveTo(moveFileInfoPath);  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 fileInfo.Delete();<\/code><\/pre>\n<p><code>FileStream<\/code> &#8212;  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>FileStream<\/code>:<\/p>\n<pre><code class=\"cs\">string fileStreamPath = @\"C:\\filestream_example.txt\";  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0444\u0430\u0439\u043b using (FileStream fs = new FileStream(fileStreamPath, FileMode.Create)) {     byte[] data = Encoding.UTF8.GetBytes(\"Hello, FileStream!\");     fs.Write(data, 0, data.Length); }  \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 using (FileStream fs = new FileStream(fileStreamPath, FileMode.Open, FileAccess.Read)) {     byte[] data = new byte[fs.Length];     int numBytesToRead = (int)fs.Length;     int numBytesRead = 0;      while (numBytesToRead > 0)     {         int n = fs.Read(data, numBytesRead, numBytesToRead);          if (n == 0)             break;          numBytesRead += n;         numBytesToRead -= n;     }      string text = Encoding.UTF8.GetString(data);     Console.WriteLine(text); }  \/\/ \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c async Task WriteAsync(string path, string content) {     byte[] encodedText = Encoding.UTF8.GetBytes(content);     using (FileStream sourceStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None, 4096, true))     {         await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);     }; }  async Task&lt;string> ReadAsync(string path) {     using (FileStream sourceStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))     {         byte[] buffer = new byte[1024];         int numRead;         StringBuilder sb = new StringBuilder();         while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)         {             string text = Encoding.UTF8.GetString(buffer, 0, numRead);             sb.Append(text);         }         return sb.ToString();     } }  \/\/ \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 await WriteAsync(fileStreamPath, \"Async Hello, FileStream!\"); string asyncContent = await ReadAsync(fileStreamPath); Console.WriteLine(asyncContent);<\/code><\/pre>\n<p><code>Directory<\/code> \u0438 <code>DirectoryInfo<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439. \u041a\u0430\u043a \u0438 <code>File<\/code> \u0438 <code>FileInfo<\/code>, <code>Directory<\/code> &#8212; \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441, \u0430 <code>DirectoryInfo<\/code> &#8212; \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <code>Directory<\/code>:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 string directoryPath = @\"C:\\example_dir\"; Directory.CreateDirectory(directoryPath);  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 if (Directory.Exists(directoryPath)) {     Console.WriteLine(\"\u0414\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 string[] files = Directory.GetFiles(directoryPath); foreach (string file in files) {     Console.WriteLine(file); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 string[] directories = Directory.GetDirectories(directoryPath); foreach (string dir in directories) {     Console.WriteLine(dir); }  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 Directory.Delete(directoryPath, true);<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440<code>DirectoryInfo<\/code>:<\/p>\n<pre><code class=\"cpp\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 DirectoryInfo DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 directoryInfo.Create();  \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 if (directoryInfo.Exists) {     Console.WriteLine(\"\u0414\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.\"); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) {     Console.WriteLine(fileInfo.Name); }  \/\/ \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0439 DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories(); foreach (DirectoryInfo dirInfo in directoryInfos) {     Console.WriteLine(dirInfo.Name); }  \/\/ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 directoryInfo.Delete(true);<\/code><\/pre>\n<h2>\u0427\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 FileStream  <\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FileStream<\/code> \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u043e\u0432 <code>FileMode<\/code>, \u0434\u043e\u0441\u0442\u0443\u043f\u0430 <code>FileAccess<\/code> \u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>FileShare<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432:<\/p>\n<pre><code class=\"cs\">\/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 string path = @\"C:\\example.txt\"; using (FileStream fs = new FileStream(path, FileMode.Create)) {     byte[] info = new UTF8Encoding(true).GetBytes(\"This is some text in the file.\");     fs.Write(info, 0, info.Length); }  \/\/ \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {     byte[] b = new byte[1024];     UTF8Encoding temp = new UTF8Encoding(true);     while (fs.Read(b, 0, b.Length) > 0)     {         Console.WriteLine(temp.GetString(b));     } }<\/code><\/pre>\n<p><code>FileMode.Create<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u043d, \u0435\u0441\u043b\u0438 \u043e\u043d \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442. <code>FileMode.Open<\/code> \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0444\u0430\u0439\u043b, \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>FileNotFoundException<\/code>.<\/p>\n<p>\u0427\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Read<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u0445 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<pre><code class=\"cs\">using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {     byte[] buffer = new byte[1024];     int bytesRead;     while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)     {         Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Write<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0430\u0439\u0442\u043e\u0432 \u0432 \u0444\u0430\u0439\u043b.<\/p>\n<pre><code class=\"cs\">using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) {     byte[] info = new UTF8Encoding(true).GetBytes(\"More text to add to the file.\");     fs.Write(info, 0, info.Length); }<\/code><\/pre>\n<p><strong>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0440\u0435\u0436\u0438\u043c\u0430\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/strong><\/p>\n<p><code>FileAccess<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0444\u0430\u0439\u043b\u0443: \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u0435\u043d\u0438\u0435 <code>FileAccess.Read<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u044c <code>FileAccess.Write<\/code>, \u0438\u043b\u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c <code>FileAccess.ReadWrite<\/code>.<\/p>\n<p><code>FileShare<\/code> \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043a \u0444\u0430\u0439\u043b\u0443: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>FileShare.Read<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b, \u043d\u043e \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e.<\/p>\n<pre><code class=\"cs\">\/\/ \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) {     \/\/ \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445     byte[] buffer = new byte[1024];     int bytesRead = fs.Read(buffer, 0, buffer.Length);     Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));      \/\/ \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445     byte[] info = new UTF8Encoding(true).GetBytes(\"Additional data\");     fs.Write(info, 0, info.Length); }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u044e\u0437\u0430\u0442\u044c <code>FileStream<\/code> \u0441 <code>FileAccess.ReadWrite<\/code> \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FileShare.Read<\/code>.<\/p>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 C#<\/h2>\n<p>\u0412 .NET <code>Stream<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0430\u0439\u0442\u043e\u0432. \u0412\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 .NET \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 <code>Stream<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f <code>FileStream<\/code>, <code>MemoryStream<\/code>, <code>NetworkStream<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <code>Stream<\/code>:<\/p>\n<ul>\n<li>\n<p><code>Read(byte[] buffer, int offset, int count)<\/code>: \u0447\u0438\u0442\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0445 \u0432 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<\/li>\n<li>\n<p><code>Write(byte[] buffer, int offset, int count)<\/code>: \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437 \u0431\u0443\u0444\u0435\u0440\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p><code>Seek(long offset, SeekOrigin origin)<\/code>: \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p><code>Flush()<\/code>: \u043e\u0447\u0438\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0431\u0443\u0444\u0435\u0440\u044b \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>Stream<\/code>:<\/p>\n<pre><code class=\"cs\">using System; using System.IO;  class Program {     static void Main()     {         string path = \"example.txt\";          using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))         {             byte[] info = new UTF8Encoding(true).GetBytes(\"Hello, Stream!\");             fs.Write(info, 0, info.Length);         }          using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))         {             byte[] b = new byte[1024];             UTF8Encoding temp = new UTF8Encoding(true);             while (fs.Read(b, 0, b.Length) > 0)             {                 Console.WriteLine(temp.GetString(b));             }         }     } }<\/code><\/pre>\n<p><code>StreamReader<\/code> \u0438 <code>StreamWriter<\/code> \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. <\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-425932","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/425932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425932"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/425932\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}