In this lecture, we will be creating two functions, one of them for changing our working directory,
and other one for listing files and folders in our directory. To change directory
we will be using setcurrentdirectory method of directory class and for listing files and folders
in directory, we will be using enumeratedirectories, enumeratefiles methods of directory class
and a String Builder class and Select Clauses.
If you are new to this concept, I highly encourage you to learn more about keywords before keeping continue.
Let's start coding.
We need to add two more functions to our operations class.
Let's do it. We will create a function called setworkingdirectory which will take path as
argument
This is a destination path where we are going.
And a function called a enumworkingdirectory for listing files and folders.
And we need to add.
Two more statements.
in our commandparser function.
Else if, if our command contains cd keyword, we will call the setworkingdirectory function with its argument
And one more statement, else if our command contains ls keyword
we will be calling
enumworkingdirectory function.
Yep we are done here, so let's go back to our functions.
For setting a new directory, we will be using setcurrentdirectory method of the directory class.
To do so, we need to use a system.io namespace and directory class is here.
We will use setcurrentdirectory method of directory class with the path argument.
That was the easy part, and for enumerating lists and enumerating folders and directories in our current
directory, we will be using
enumeratedirectories method of directory class and enumeratefiles method of directory class, it will be something
like this system.io directory
enumeratedirectories
It takes a path as argument.
So we have, but it returns a collection type.
Here it is. To do so, we need to assign to the var type variable.
I will call it dirs.
And in order to select every line in this collection, we will be using select clauses to use
select clauses, please add system.Linq namespace at the top of your file.
Now we can use. Select clauses has its own syntax, it just like SQL queries. From anything lets call
line
Line in the output of this method, we will select every line and we have assigned every line of the collection
into the dirs variable.
So we can access them in a loop like for each loop, a variable lets call it dir in dirs
So every time this loop iterates, dir variable, will be equals to a line of the dirs collection.
So we need to append every dir variable to a string.
To do so, we will be using stringbuilder class. To use it
we will be using system.text namespace
Let's call it sbInstance stringbuilder instance
and we will append
every line to our instance.
And then in order to get our data line by line, we will append new line
after every line.
To do so can kindly use the newline property of the environment class.
When the loop is over, we can turn that sbInstance into string by using ToString method.
So it's something like this string of enumeratedDirectories will be equal to sbInstance.ToString()
So it will be something like this, C:Windows, C:\program files and everyline seperated
by a new line.
etc. And for the files, we will do the same thing, just copy.
just copy every line
and paste it
and.
Create a new instance of the string builder, I will call it Instance2
and use the instance2 in everywhere and change dirs with files and change the methods.
we will be using enumeratefiles method
this time.
enumeratefiles method.
Actually, we do not need to use a second instance of the stringbuilder class, so in the first loop our output
will be something like this
all directories.
Windows, etc., and in the next loop, we will list files, we will append files to the string
To do so
simply keep continue to using first sbInstance
And I append every file to our sbInstance, it's OK.
We will append every file and after every file we will append a new line so our output keep continue to
separated by a new line in every line, and at the end of all of the loops, we will turn sbInstance
to string.
Let's call dirs and files will be equals to sbInstance
ToString.
And our function will return this value.
So we need to change the return type it will be string.
So everything's are OK.
Everythings are seems OK, and let's test our code.
Let's see the output of our enumworkingdirectory function by using console.writeline method inside the
commandparser function.
And it will print out the return value of the enumworkingdirectory function, which is the new string we
have created.
Let's test it.
We will call commandparser function and we will say
ls
C:\
so it should have return the all files and directories under the C drive.
Let's test it.
As you can see from the output, you can see all the directories and files under C drive perfect, we are
done here.
That's it for this lecture.
See you in the next one.