Advertisement
Mirineo

FilesAndExceptionsLecture

Feb 9th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. namespace FilesAndExceptions
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Linq;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main(string[] args)
  11.         {
  12.             //if (!File.Exists("myfile.txt"))
  13.             //{
  14.             //    File.Create("myfile.txt");
  15.             ////}
  16.  
  17.             ////var text = File.ReadAllText("myfile.txt");
  18.  
  19.             ////File.Move("myfile.txt", "../anotherfile.txt"); // renaming and moving a file
  20.  
  21.             //Console.WriteLine(text);
  22.  
  23.             //var file = "myfile.txt";
  24.  
  25.             //var lines = File.ReadAllLines(file);
  26.  
  27.             //var oddLines = new List<string>();
  28.  
  29.             //if (!File.Exists("result.txt"))
  30.             //{
  31.             //    File.Create("result.txt");
  32.             //}
  33.  
  34.             //for (int i = 0; i < lines.Length; i++)
  35.             //{
  36.             //    if (i % 2 != 0)
  37.             //    {
  38.             //        oddLines.Add(lines[i]);
  39.             //    }
  40.             //}
  41.  
  42.             //File.WriteAllLines("result.txt", oddLines);
  43.  
  44.             //var lines = File.ReadAllLines(file);
  45.  
  46.             //var linesWithNumbers = new List<string>();
  47.  
  48.             //for (int i = 0; i < lines.Length; i++)
  49.             //{
  50.             //    linesWithNumbers.Add($"{i + 1}. {lines[i]}");
  51.             //}
  52.  
  53.             //File.WriteAllLines("result.txt", linesWithNumbers);
  54.  
  55.             //var words = File.ReadAllText("words.txt")
  56.             //    .Split(new[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries)
  57.             //    .Select(s => s.ToLower())
  58.             //    .ToArray();
  59.  
  60.             //var text = File.ReadAllText("text.txt").Split(new[] { ' ', '.', ',', '!', '?', '-', '\'', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
  61.             //    .Select(s => s.ToLower())
  62.             //    .ToArray();
  63.  
  64.             //var result = new Dictionary<string, int>();
  65.  
  66.             //for (int i = 0; i < words.Length; i++)
  67.             //{
  68.             //    var currentWordCount = 0;
  69.             //    var currentWord = words[i];
  70.  
  71.             //    for (int j = 0; j < text.Length; j++)
  72.             //    {
  73.             //        if (currentWord == text[j])
  74.             //        {
  75.             //            currentWordCount++;
  76.             //        }
  77.             //    }
  78.  
  79.             //    result[currentWord] = currentWordCount;
  80.             //}
  81.  
  82.             //var sortedResult = result
  83.             //    .OrderByDescending(kvp => kvp.Value)
  84.             //    .Select(kvp => $"{kvp.Key} - {kvp.Value}")
  85.             //    .ToArray();
  86.  
  87.             //File.WriteAllLines("result.txt", sortedResult);
  88.  
  89.             //var dir = Directory.GetCurrentDirectory();
  90.  
  91.             //var dirInfo = new DirectoryInfo(dir);
  92.  
  93.             //var parent = dirInfo.Parent;
  94.  
  95.             //var allDirsInParent = parent.GetDirectories();
  96.  
  97.             //foreach (var dirc in allDirsInParent)
  98.             //{
  99.             //    Console.WriteLine(dirc);
  100.             //}
  101.  
  102.             //var totalLength = Directory.GetFiles("Debug")
  103.             //    .Select(f => new FileInfo(f))
  104.             //    .Sum(f => f.Length/ 1024.0 / 1024.0);
  105.  
  106.             //double sum = 0;
  107.  
  108.             //foreach (var file in files)
  109.             //{
  110.             //    FileInfo fileInfo = new FileInfo(file);
  111.             //    sum += fileInfo.Length;
  112.             //}
  113.            
  114.  
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement