C# Tutorial for Beginners #36 - File IO (Reading, Writing, and Manipulating)

Поделиться
HTML-код
  • Опубликовано: 6 июн 2024
  • Using the File class to read files, write files, append to files, as well as exists, copy, move/rename, and delete functions. Also tips on the File class, and how to use the Path class to easily combine paths.
    If you are feeling adventurous, try writing and appending lines from a string array to your file with File.WriteAllLines and File.AppendAllLines!
    Please let me know if you have any questions. Happy coding!
    Chapters:
    0:00 Intro
    0:22 Path.Combine
    2:06 Writing a File
    3:12 Appending to a File
    4:19 Reading a File
    4:59 Reading Lines from a File
    5:58 Exists
    6:34 Copying a File
    7:34 Moving/Renaming a File
    8:35 Deleting a File
    9:53 File Class Tips
    10:39 Future Considerations
    11:22 Next Up
  • НаукаНаука

Комментарии • 8

  • @youssefyoussef759
    @youssefyoussef759 Год назад +1

    Thank you for this information 🎉🎉🎉🎉

    • @KampaPlays
      @KampaPlays  Год назад +1

      Glad to help, thank you for watching!!

  • @MethodOverRide
    @MethodOverRide Год назад +3

    Just want to say thanks again for this video and the series. I used these methods at work recently. I needed to create a script to update the delimiter of files from tab to bar and zip the files. I knew of these methods from your video and that I could use Powershell to access them. This was part of a larger project I'm working on and my C# / .Net knowledge has helped me quite a bit recently. I even earned a small award at work because of my efforts. So thanks again for the series and I'm looking forward to the next one.

    • @KampaPlays
      @KampaPlays  Год назад +1

      That is awesome to hear, and excellent job on picking it up and putting it to good use. Congrats on the award!! Thank you for letting me know, I'm really happy that it's been useful.

  • @MethodOverRide
    @MethodOverRide Год назад +4

    I've been using stream reader and stream writer to read and write flat files. Does the file class offer any benefits or draw backs?

    • @KampaPlays
      @KampaPlays  Год назад +1

      Excellent question!
      Behind the scenes, the File class's methods use stream writers/readers, so unless you handle your stream poorly, the performance will be extremely similar in basic use cases.
      In my opinion the biggest benefit of the File class is in simplicity. If you have smaller use cases like writing or reading a reasonable amount of text, then the File class can achieve the same results in a simple way. Simpler to write, but more importantly simpler to read and understand what is happening at a glance.
      That being said, the File class is definitely not an end-all solution to file IO, and it's great that you are familiar with streams. If you are dealing with large amounts of data, or if you are sending or receiving data in [near] real-time, you absolutely need a stream so that you can maintain control of your file, and its read/write actions and buffers. These things would be impractical to impossible with the File.WriteAllText/ReadAllText methods.

    • @MethodOverRide
      @MethodOverRide Год назад +2

      @@KampaPlays I appreciate the response! I've been coding stream writer and readers with the using keyword. My understanding is that the using keyword will take care of resource management. I try to use the using keyword for any type of stream(?) because I have no idea yet how to properly deal with memory management. I'm only building basic console apps for some of my ETL tasks, but I am worried about crashing our server because of a memory leak. 😅

    • @KampaPlays
      @KampaPlays  Год назад +1

      No problem! Sounds like you are already dealing with your memory management! 🙂 Generally C# objects will fall out of scope and the garbage collector will dispose of them for you, but yes, streams do not work that way. Streams (and certain other types) need to hold various items (file handles, buffers, etc) in memory and usually implement the IDisposable interface to allow you to dispose (with .Dispose() or by the use of a using statement) of the object when you are done with it so you don't leak. When you aren't familiar with a class, it's a good idea to check the .NET API reference for things like these -- generally the example code will be a good indicator of its safe usage, but you can also drill into its parents and see what all it implements!