Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace TestFileStreams
- {
- class FileDir
- {
- static void Main()
- {
- //Да се организира меню за избор на една от специалните директории (0 за край)
- Console.WriteLine("Which directory would you like to look at?");
- Console.WriteLine("1 for Desktop");
- Console.WriteLine("2 for Documents");
- Console.WriteLine("3 for Music");
- Console.WriteLine("4 for Pictures");
- Console.WriteLine("5 for Videos");
- Console.WriteLine("0 for EXIT");
- string folderName="";
- int userChoice = int.Parse(Console.ReadLine());
- switch (userChoice)
- {
- case 0:
- {
- break;
- }
- case 1:
- {
- folderName = "Desktop";
- break;
- }
- case 2:
- {
- folderName = "MyDocuments";
- break;
- }
- case 3:
- {
- folderName = "MyMusic";
- break;
- }
- case 4:
- {
- folderName = "MyPictures";
- break;
- }
- case 5:
- {
- folderName = "MyVideos";
- break;
- }
- }
- if (folderName != "")
- {
- Environment.SpecialFolder sf = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folderName);
- Console.WriteLine("File name?");
- StreamWriter sw = new StreamWriter(@"E:\"+Console.ReadLine()+".txt");
- using (sw)
- {
- GetFilesAndFolders(sw,Environment.GetFolderPath(sf));
- }
- //Да се попита потребителя за име на файл, в който да се запише резултата
- }
- }
- public static void GetFilesAndFolders(StreamWriter sw,string path)
- {
- string[] dirs = Directory.GetDirectories(path);
- sw.WriteLine("Starting from:{0}\nDirectories:\n", path);
- foreach (string d in dirs)
- {
- sw.WriteLine(d);
- }
- sw.WriteLine();
- sw.WriteLine("Files in current directory: ");
- sw.WriteLine();
- string[] files = Directory.GetFiles(path);
- foreach (string d in files)
- {
- sw.WriteLine(d);
- }
- foreach (string d in dirs)
- {
- GetFilesAndFolders(sw, d);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment