Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Surf directory - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

Surf directory - Practice Exercises C# Sharp


Lesson 12:

Additional libraries


Exercise 12.9:

Surf directory


Objetive:

Create a program to display the files in the current directory, and to allow the user to move up and down in that list. If the user presses Enter on a directory name, he will enter that directory; if he presses Enter on a file, that file will be launched.


Source Code:


using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections;
class SurfDirectory
{
    static int position = 0;

    static List items;

    static string directory = ".";

    static void Main()
    {
        while (true)
        {
            Console.Clear();

            items = GetItems(directory);

            ShowItems();
            ShowIndications();
            ReadKeys();
            Thread.Sleep(200);
        }
    }

    static void OpenFile(Item item)
    {
        if (item.IsFile)
        {
            Process.Start(item.Name);
        }
    }

    static void ReadKeys()
    {
        ConsoleKeyInfo key = Console.ReadKey();
        switch (key.Key)
        {
            case ConsoleKey.UpArrow:
                if (position > 0)
                {
                    position--;
                }
                break;
            case ConsoleKey.DownArrow:
                if (position < items.Count - 1)
                {
                    position++;
                }
                break;
            case ConsoleKey.Enter:
                Item item = items[position];

                if (item.IsFile)
                {
                    OpenFile(item);
                }
                else
                {
                    directory = item.Path;
                }
                break;
        }
    }

    static void ShowSelected(int i)
    {
        if (i == position)
        {
            Console.SetCursorPosition(0, position);
            Console.BackgroundColor = ConsoleColor.DarkCyan;
        }
        else
        {
            Console.BackgroundColor = ConsoleColor.Black;
        }
    }

    static List GetItems(string direc)
    {
        try
        {
            List items = new List();

            string[] directories = Directory.GetDirectories(direc);
            foreach (string directory in directories)
            {
                items.Add(new Item(directory, false));
            }

            string[] files = Directory.GetFiles(direc);
            foreach (string file in files)
            {
                items.Add(new Item(file, true));
            }

            return items;
        }
        catch
        {
            Console.WriteLine("Error reading items.");
            return null;
        }
    }

    static void ShowItems()
    {
        int i = 0;
        foreach (Item item in items)
        {
            ShowSelected(i);
            Console.WriteLine(item.Path);
            i++;
        }
        Console.BackgroundColor = ConsoleColor.Black;
    }

    static void ShowIndications()
    {
        Console.SetCursorPosition(0, 23);
        Console.WriteLine("Press arrow up for move up  | Press arrow down for move down");
    }
}

public class Item
{
    public string Path { get; set; }

    public bool IsFile { get; set; }

    public string Name
    {
        get { return Path.Substring(2); }
    }

    public Item(string path, bool isFile)
    {
        this.Path = path;
        this.IsFile = isFile;
    }
}
Exercisey 12.9

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.