Advertisement
GodsProphet

MyDir

Dec 14th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using static System.Console;
  6.  
  7. namespace _1412_18
  8. {
  9.     class MyDir
  10.     {
  11.         public static void Save(FileInfo fi) {
  12.             using (FileStream fs = fi.Open(FileMode.Create, FileAccess.Write, FileShare.None))
  13.             {
  14.                 string str = "Несе Галина воду";
  15.                 byte[] bs = Encoding.Unicode.GetBytes(str);
  16.                 fs.Write(bs, 0, bs.Length);
  17.                 WriteLine($"File {fi.FullName} save");
  18.             }
  19.         }
  20.  
  21.         public static string  Load(FileInfo fi)
  22.         {
  23.             using (FileStream fo = fi.OpenRead())
  24.             {
  25.                 byte[] bs = new byte[fo.Length];
  26.                 fo.Read(bs, 0, bs.Length);
  27.                 string str =Encoding.Unicode.GetString(bs);
  28.                 WriteLine($"File {fi.FullName} is read");
  29.                 return str;
  30.             }
  31.         }
  32.  
  33.         static void InfoDisk()
  34.         {
  35.             DriveInfo[] drives = DriveInfo.GetDrives();
  36.             foreach (DriveInfo item in drives)
  37.             {
  38.                 WriteLine($"Name {item.Name} ");
  39.                 WriteLine($"DriveType {item.DriveType} ");
  40.                 if (item.IsReady)
  41.                 {
  42.                     WriteLine($"TotalSize {item.TotalSize} ");
  43.                     WriteLine($"TotalFreeSpace {item.TotalFreeSpace} ");
  44.                     WriteLine($"VolumeLabel {item.VolumeLabel} ");
  45.                     WriteLine($"RootDirectory {item.RootDirectory} ");
  46.                 }
  47.  
  48.             }
  49.  
  50.  
  51.  
  52.         }
  53.  
  54.         static void DirInfo()
  55.         {
  56.             DirectoryInfo dir = new DirectoryInfo(@"d:\");
  57.             if (dir.Exists)
  58.             {
  59.                 WriteLine(dir.FullName);
  60.                 WriteLine(dir.CreationTime);
  61.                 foreach (var item in dir.GetDirectories())
  62.                 {
  63.                     WriteLine("folder -> "+ item);
  64.                 }
  65.                 foreach (var item in dir.GetFiles("*.jpg"))
  66.                 {
  67.                     WriteLine("file -> " + item);
  68.                 }
  69.  
  70.             }
  71.             else
  72.             {
  73.                 WriteLine("Not foud");
  74.             }
  75.  
  76.  
  77.         }
  78.         public static void Run() {
  79.             //DirInfo();
  80.             InfoDisk();
  81.  
  82.  
  83.             DirectoryInfo dir = new DirectoryInfo(@"d:\test");
  84.             if (!dir.Exists)
  85.                 dir.Create();
  86.             DirectoryInfo dir1 = dir.CreateSubdirectory("subdir1");
  87.             WriteLine(dir1.FullName);
  88.             FileInfo finfo = new FileInfo(dir1+@"\new.bin");
  89.             Save(finfo);
  90.             WriteLine(Load(finfo));
  91.  
  92.             //string newfolder = @"D:\test10";
  93.             //if (Directory.Exists(newfolder) == false)
  94.             //    dir.MoveTo(newfolder);
  95.             try
  96.             {
  97.                 dir1.Delete(true);
  98.             }
  99.             catch (Exception e) {
  100.                 WriteLine(e.Message);
  101.             }
  102.  
  103.  
  104.             string fname = "test.txt";
  105.  
  106.             FileInfo iffile = new FileInfo(fname);
  107.  
  108.             if (iffile.Exists)
  109.             {
  110.                 WriteLine("FullName -> " + iffile.FullName);
  111.                 WriteLine("CreationTime -> " + iffile.CreationTime);
  112.                 WriteLine("Size -> " + iffile.Length);
  113.  
  114.                 // string text = File.ReadAllText(iffile.FullName);
  115.                 //WriteLine(text);
  116.  
  117.                 string[] arrstr = File.ReadAllLines(iffile.FullName);
  118.                 foreach (var item in arrstr)
  119.                 {
  120.                     WriteLine(item);
  121.                 }
  122.                 iffile.Delete();
  123.                 WriteLine($"File {fname} is deleted");
  124.             }
  125.             else {
  126.                 string[] arrstr = { "Я", "не", "виконую", "домашню", "роботу", ", так як", "я", "ледарюга", "!!!" };
  127.                 File.WriteAllLines(fname,arrstr);
  128.                 //string tt = "Жили у бабусі два веселих гусі";
  129.                 //File.WriteAllText(iffile.FullName, tt);
  130.  
  131.                 File.AppendAllText(iffile.FullName, "І ще два гусі");
  132.  
  133.  
  134.                 WriteLine($"File {fname} is created");
  135.  
  136.             }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.         }
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement