Working With The File System in C# – Managing Folders and Files


Full courses:
Blog Post:
Newsletter signup:

C# can be a really powerful tool for working with the file system. It can automate file tasks including backup creation, organizing files, monitoring file info, and more. In this video, we are going to get started working with files and directories.


Xem Thêm Bài Viết Khác:


  1. Morning,

    Thank you for a lecture, this code seems clear and easy until I tried to use it in my project. The problem I faced is I want to display it in a Textbox or Listbox but now "Path" refuses all the visual studio recommending references and it stays an error.

    Can anyone please assist? I do not want to use Console to display as in this project above.

    Thank you,

  2. Thanks for the video! Is there any chance that we can know what document was last modified in Directory, and based on that open that file for writing?

  3. Nice Video. I've been away from my learning journey and had to get back to some basic stuff.
    Things I would like to point out (questions and also some tips to improve people learning – like me):

    " File.Move() "
    private static string FileMove(string source, string destination) { /* Code Here */ }

    Note: you can call the method and send the source and destination "full path"

    Q1: I never understood properly the string interpolation. Now I understand a little bit. Is it a "best practice" compared to my example above?

    Also for File.Move(): File.Move is supposed to also perform renaming (something I am struggling at the moment. I simply cant accomplish it. This could be covered in the video.
    Q2: How to perform a rename?

    Finally some other tip:

    A returning method is also useful (i.e: Checking if file or Directory exists):
    private static bool FileExists(string file) { return File.Exists(specifiedDirectory); } // or "string(root)" | root – as used in this tutorial

    Just call:

    if (FileExists("c:myfile.txt")) { /* do something */ }

     Both Thumbs Up for this tutorial.

  4. Good video, sir. I have been using only the tip of the file services with projects at work. I had an "aha!" moment. I have been getting the file names, the long way, by searching for the last slash, and removing everything up to the last slash. I am looking forward to using these newly acquired things for my application.

  5. this might help me hehe 😀
    am actually trying to make a "Simpel" multi .exe file run via several buttons, but to be modular sih i think i need to have a "Settings" form where i have a "brow`s" button for each file that i guess need to save it to a file .cfg or .ini
    my idea whats to make a multi emulator launcher ;D

  6. There seems to be a limit to how many files and/ or sub directories run through. I can get this to work fine with TopDirectoryOnly but I do not get any results with AllDirectories on a subdirectory which has 50 folders and 4K files…..

  7. Hey, thank you for the video, can you help me and give me some dirction on how to open my program from a file in my desktop and then let the program read whats in the file?

  8. Nice tutorial, how to get file name in c folder like documents and setting, it said access denied. How to get the permission? Do you have the tutorial?

  9. Gotta love your videos, Tim. So far, each and every one of them has been amazingly useful. I like the pace and the fact that you know your thing by explaining why things are the way they are. I'm still in the learning process after two months of hard work because I love programmig. Though I'm a Mechanical Engineer, I've got my mind set on the idea of becoming a software engineer (back-end or something in between related to mechanical engineering, like IoT). I've made it until delegates, reflection and LINQ. Any recommendation on what to do next? I've already started a MySQL course and I'm finishing a desktop application that connects Autocad (a 2D design program), Excel (the pricing is downloaded into a PowerPivot table connected to SQL, basic queries, no rocket science) and a final quotation (displaying images and the corresponding wording whenever possible, you know, just for standardized solutions). You know, learning by practising. If you have any ideas on what steps to take next, I'll be more than happy to read them, if not, thanks for these great vids. Keep it up!

  10. I've used this Windows Application for the Users Files, the path was stored in the database for every user.

    Days long I couldn't figure out how to put those files in the ImageList so they can look pretty same as windows explorer 🙂

  11. Hi Tim, great tutorial on the file system basics.

    At around 23:45 you mention using string interpolation to combine destination folder, slash character and file name. I find using Path.Combine is helpful as it takes care of adding the slash character if necessary, e.g.

    string destinationFileName = Path.Combine(rootPath, "SubFolderA", Path.GetFileName(file));

  12. Hello. I am afraid I didn't understand some of those manipulations with search patterns. I understand, that "*s" means that we will find all files that end with "s". "t*" means that we will find all files that start with "t". And what does "*.*" mean? That we will find all files that have "." somewhere in the middle of their names?

  13. Long back when working with files, I had a long path that system was not accepting it. But the same time i have noted window was handling those paths with ~ symbol for making the path short. How it is possible in program

  14. Tim, this is awesome! This is perfect for some file maintenance issues that I sometimes have to periodically perform. I used to do a lot of this manually years ago and then wrote routines using VBA that does it pretty well but now I will write this in C# because who wants to use VBA anymore!

  15. Just one complaint. Green font on black console is reeeaallllyyyy hard to see. Especially the first line at the top. Either bump up the font or change the font color to white or any color that has great contrast with black.

  16. You know something like this would be perfect for MAC, more often than not when you uninstall something it still leaves the old files in the Library/Cache or ~Library/Cache area. I've literally just saved 13GB by emptying those files. Ironically, I don't get that many problems on Windows, but on Mac(for me at least), it's a massive problem – so writing a programme like that would be ideal.

    Thing is, I sit at work all day programming, so to come home and do more programming after work, can sometimes take motivation! 😉


Please enter your comment!
Please enter your name here