Question: I am trying to create a toggle switch described as [w3school](https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch). But I want to be my slider look like [![ok](https://i.stack.imgur.com/luPLw.png)](https://i.stack.imgur.com/luPLw.png) [![notok](https://i.stack.imgur.com/cL1Hf.png)](https://i.stack.imgur.com/cL1Hf.png) 

All I can come up with 

[![enter image description here](https://i.stack.imgur.com/RDn1i.png)](https://i.stack.imgur.com/RDn1i.png) [![enter image description here](https://i.stack.imgur.com/iAQrG.png)](https://i.stack.imgur.com/iAQrG.png)

Background color is not important the position of the slider is.
Any help is appreciated.

Answer: 
There are two things you can do to improve that code:

1. Use `Directory.EnumerateFiles()` and `Directory.EnumerateDirectories()` to avoid making a copy of the names of all the files in each directory.
2. Make the return type of the method `IEnumerable<string>` to make it easier to consume.

We also need to be very careful about exceptions caused by attempting to access protected files and directories. The code below is also complicated by the fact that you're not allowed to `yield return` from inside a `try/catch` block, so we have to rearrange the code somewhat.

(Also note that we have to dispose the enumerator returned from `.GetEnumerator()`; normally this is done automatically when you use `foreach`, but in this case we can't - because of having to avoid doing `yield return` in a `try/catch` - so we have to use `using` to dispose it.)

Here's a modification of your original code to do this:

```
public static IEnumerable<string> GetFiles(string root, string spec)
{
    var pending = new Stack<string>(new []{root});

    while (pending.Count > 0)
    {
        var path = pending.Pop();
        IEnumerator<string> fileIterator = null;

        try
        {
            fileIterator = Directory.EnumerateFiles(path, spec).GetEnumerator();
        }

        catch {}

        if (fileIterator != null)
        {
            using (fileIterator)
            {
                while (true)
                {
                    try
                    {
                        if (!fileIterator.MoveNext()) // Throws if file is not accessible.
                            break;
                    }

                    catch { break; }

                    yield return fileIterator.Current;
                }
            }
        }

        IEnumerator<string> dirIterator = null;

        try
        {
            dirIterator = Directory.EnumerateDirectories(path).GetEnumerator();
        }

        catch {}

        if (dirIterator != null)
        {
            using (dirIterator)
            {
                while (true)
                {
                    try
                    {
                        if (!dirIterator.MoveNext()) // Throws if directory is not accessible.
                            break;
                    }

                    catch { break; }

                    pending.Push(dirIterator.Current);
                }
            }
        }
    }
}

```

As an example, here's how you could use a console app to list all the accessible ".txt" files on the "C:\" drive:

```
static void Main()
{
    foreach (var file in GetFiles("C:\\", "*.txt"))
    {
        Console.WriteLine(file);
    }
}

```
Regarding the error "denied access to path (...)", sometimes you have to run Visual Studio as an a administrator in order to access some folders in the C:\ drive.