coasterka

OperationsWithFilesDirs

Mar 18th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace TestFileStreams
  8. {
  9.     class FileDir
  10.     {
  11.         static void Main()
  12.         {
  13.             //Да се организира меню за избор на една от специалните директории (0 за край)
  14.             Console.WriteLine("Which directory would you like to look at?");
  15.             Console.WriteLine("1 for Desktop");
  16.             Console.WriteLine("2 for Documents");
  17.             Console.WriteLine("3 for Music");
  18.             Console.WriteLine("4 for Pictures");
  19.             Console.WriteLine("5 for Videos");
  20.             Console.WriteLine("0 for EXIT");
  21.             string folderName="";
  22.             int userChoice = int.Parse(Console.ReadLine());
  23.             switch (userChoice)
  24.             {
  25.                 case 0:
  26.                     {
  27.                         break;
  28.                     }
  29.                 case 1:
  30.                     {
  31.                         folderName = "Desktop";
  32.                         break;
  33.                     }
  34.                 case 2:
  35.                     {
  36.                         folderName = "MyDocuments";
  37.                         break;
  38.                     }
  39.                 case 3:
  40.                     {
  41.                         folderName = "MyMusic";
  42.                         break;
  43.                     }
  44.                 case 4:
  45.                     {
  46.                         folderName = "MyPictures";
  47.                         break;
  48.                     }
  49.                 case 5:
  50.                     {
  51.                         folderName = "MyVideos";                        
  52.                         break;
  53.                     }
  54.             }
  55.             if (folderName != "")
  56.             {
  57.                 Environment.SpecialFolder sf = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folderName);
  58.                 Console.WriteLine("File name?");
  59.                 StreamWriter sw = new StreamWriter(@"E:\"+Console.ReadLine()+".txt");
  60.                 using (sw)
  61.                 {
  62.  
  63.                     GetFilesAndFolders(sw,Environment.GetFolderPath(sf));
  64.                 }
  65.                 //Да се попита потребителя за име на файл, в който да се запише резултата
  66.             }
  67.         }
  68.         public static void GetFilesAndFolders(StreamWriter sw,string path)
  69.         {
  70.             string[] dirs = Directory.GetDirectories(path);
  71.             sw.WriteLine("Starting from:{0}\nDirectories:\n", path);
  72.             foreach (string d in dirs)
  73.             {
  74.                 sw.WriteLine(d);
  75.             }
  76.             sw.WriteLine();
  77.             sw.WriteLine("Files in current directory: ");
  78.             sw.WriteLine();
  79.             string[] files = Directory.GetFiles(path);
  80.             foreach (string d in files)
  81.             {
  82.                 sw.WriteLine(d);
  83.             }
  84.             foreach (string d in dirs)
  85.             {
  86.                 GetFilesAndFolders(sw, d);
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment