C# and .NET Basics

File APIs

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "File APIs" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer demonstrates how to read and write files directly from disk using the File class, and the usage of methods such as `File.ReadAllText()`, `File.ReadAllLines()`, and `File.ReadAllBytes()`. He also mentions the FileInfo class for obtaining metadata about a file, the directory class for creating directories, and the Path class for manipulating file paths.

Preview
Close

Transcript from the "File APIs" Lesson

[00:00:00]
>> Spencer Schneidenbach: Almost to the end, just a few other kinds of APIs that I wanna talk about. So file APIs, so I mentioned streams, I was hard on streams, I was big on streams and I said, yes, you need to use streams when you don't wanna necessarily put something into memory, but you sometimes kinda do, right?

[00:00:19]
Out of convenience you're loading up a spreadsheet, it's only got a few hundred kilobytes of data, right? You don't need to worry too much about resources, it's not a 50 megabyte or 500 megabyte file. So there's a file class that you can use to read and write files directly from disk.

[00:00:37]
Take for example, there's three main methods that I like to use that I use frequently, File.ReadA\\Text, File.ReadA\\Lines, which will return a array of strings and File.ReadA\\Bytes, which will read that file as a byte array. And so this is how you call them, it's very simple, you call File.ReadA\\Text, I can show you an example of it here.

[00:01:02]
Let's go ahead and o this new file doesn't never works for me up here so I better do it right here. I'm gonna do new file file.txt and some stuff. Some more stuff and very important I need to set the properties of this thing to make sure that it copies to, actually, what I think we can just copy it to the route here.

[00:01:30]
File.text, well regardless not letting me copy paste it and that's okay. Okay, I do wanna point out that if we wanted to read this file as a this text file as a text, as a string, as just a string of the file contents, we could do file and you'll note that read all lines.

[00:01:57]
You'll note that it is just accessible sometimes you have to import system.io.file, which is fine. So I wanna say, read all read all text, actually, you'll notice that there's an async method of this. So, again, async all the way down when you can, so if you've got your file.text, and you'll notice that this reads it in as a string, and then the file read all lines.

[00:02:24]
Reads it in as a string array, and then byte of course is a byte array. Okay, so useful for some file operations not strictly speaking, something that you'll do a lot of or every day. Most of the times when you're talking, especially in the context of a web app, you're probably storing your files off on some remote Azure storage or Amazon S3 bucket or something like that where you're not even interacting with the file system.

[00:02:51]
But it's important to know that these things exist. The file API also has file.copy, file.move, file.delete, it also allows you to get info about the file. So it's sometimes important to be able to say, hey, this file exists on disk, but I need to know metadata about it.

[00:03:08]
I need to know its size I need to know the permissions that exist on it, what users can access it and in that case, the file Info class is what would be useful for that. So it has a few properties most of the time, I'm usually just worried about the path, sometimes the file length if I need to be.

[00:03:24]
But essentially what you do is you create a new copy of file info, you bring in the path of the file. If it doesn't exist, you'll get an exception otherwise, if it does exist, you'll get a fully usable object that will give you all the information that you could ever want about that object including the full path back to it.

[00:03:41]
You could even use file info.delete to delete the file and correspondingly there is a directory class that you can use to create directories. This is very useful when you're doing file processing and you need to create directories and create files. And maybe you're creating these very temporarily, or you just wanna make sure that the final resting place with this file exists.

[00:03:59]
So directory.createdirectory is something that I'll use frequently. If it exists, no action is taken so you can call this method multiple times at the same argument, which is important to know. Lastly, if you're concerned about getting paths or need to get paths to files or you need to construct paths to a file.

[00:04:21]
Maybe you have multiple directories you need to chain together to get the final resting place for the file that you need to create. You can use the path class to use these APIs, such as path combine, which will essentially create directory\file.text, so it automatically adds the operating systems way of separating those files.

[00:04:41]
You can get the files extension, or you can even get a random or temporary file name and that could be for something that you just store locally for a very small amount of time. And I do this frequently, especially with deployed applications for my clients, where I say, I need to do some quick processing, I need to download this file real quick.

[00:04:59]
I'm just gonna borrow a little disk space, I'm not gonna borrow memory. I'm just gonna borrow some disk space from you, but I'll release the file after I'm done and after much testing and sometimes back and forth with the code, you'll actually get it to do that. So file APIs, sometimes useful, not necessarily super important but it's there if you need it.

[00:05:21]
>> Student: Are there any techniques to make sure you're able to unlock or release a file even if your program crashes?
>> Spencer Schneidenbach: If your program crashes, it's gonna release the handle on that file, yeah.
>> Student: And do you have any recommendations for any LDAP or Active Directory libraries that are commonly used?

[00:05:38]
>> Spencer Schneidenbach: I do not, that is not something I've had to touch for a long time.
>> Student: Thankfully.
>> Spencer Schneidenbach: Thankfully, I didn't want to say, but you said it for me. Yeah, I haven't had to touch LDAP or Active Directory in a long time, I do believe, .NET Framework, when I've had to do that back in the day, had a pretty rich experience.

[00:05:55]
So I would assume those APIs made their way to the core library.
>> Student: If a user does not have elevated permissions to create a directory to the C or C temp, how do we get around that.
>> Spencer Schneidenbach: If they don't have permissions to do so, typically, you have permissions to write to some directory, some temporary director.

[00:06:17]
Not usually, not typically the root of C unless you're elevated, is there a way to get around that? No, make sure that your program is executing under the correct user context. But typically I've not found that creating temporary files has been problematic for me.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now