Using System.IO to Create, Read, Rename and Delete Files and Folders in C# and Visual Basic

If you’re making use of C# or Visual Basic for Windows Forms, ASP.NET Web Forms or ASP.NET MVC, you have access to a very powerful file-system access library that’s included with Visual Studio. The library, System.IO, will allow you to create, rename, edit and delete files and folders on your system. If you are using System.IO in ASP.NET, you will be able to access the file system on the web-server assuming that appropriate permissions are set with the Network Service account. For the purposes of this article, all of the code will be in C#, but rest assured, the same functionality is available in Visual Basic, but the syntax might be slightly different.

To get started with the library, add a reference to the System.IO library at the top of your file

Using System.IO;

Here’s how to manipulate folders/directories using the System.IO Library:

Delete a folder:

Directory.Delete(“c:\\directory\\subdirectory\\”); //standard delete

Directory.Delete(“c:\\directory\\subdirectory\\”, true); //delete folder and all subdirectories

Create a folder:

Directory.CreateDirectory(“c:\\directory\\subdirectory\\”)

Check if a folder exists:

Directory.Exists(“c:\\directory\\subdirectory\\”); //returns boolean value

Move/Rename a folder:

Directory.Move(“c:\\directory\\oldlocation\\”, “c:\\directory\\newlocation\\”);

Get a list of files in a folder:

Directory.GetFiles(“c:\\directory\\subfolder\\”); // returns a string array of files in the folder

Here’s how to manipulate files using the System.IO Library:

Delete a file:

File.Delete(“c:\\directory\\filename.text”);

Create a file:

File.Create(“c:\\directory\\filename.text”); //note, the file will be empty. There are some overloads available that will let you create a file with content in it

Check if a file exists:

File.Exists(“c:\\directory\\subdirectory\\filename.text”); //returns boolean value

Move/Rename a file:

File.Move(“c:\\directory\\filename.text”, “c:\\directory\\filenamenew.text”);

Create a copy of a file:

File.Copy(“c:\\directory\\filename.text”, “c:\\directory\\filenamenew.text”);

Read text from file:

string FileContents = File.ReadAllText(“c:\\folder\\filename.text”);

Write text to a file:

File.WriteAllText(“c:\\filename.text”, “contents of file”);

Tips for Using System.IO in ASP.NET

Using Server.MapPath to Convert Relative URLS to Physical File Paths

Remember that the System.IO library always requires that you input a physical file path on the web server’s hard drive. Passing in a file or directory relative to the root of the web server will not work. IE, File.Exists(“/images/file.jpg”) will result in an exception. Fortunately, Microsoft has recognized that this would be an issue ahead of time and has given us the “Server.MapPath” function which will convert relative URLs on the server into physical file paths.

Here’s an example of Server.MapPath in action:

string RelativeURL = "/media/files/mypicture.jpg";
string AbsolutePath = Server.MapPath(RelativeURL);
bool FileExists = File.Exists(AbsolutePath);

Setting File and Directory Permissions in IIS for ASP.NET

If you plan on doing any sort of uploading files, modifying files, deleting files, modifying folders or deleting folders from your web-server, you are going to have to assign additional permissions on the server. To do this, you will need to make modifications to the appropriate folders that you want to be able to modify in IIS or have your web host do it for you. Essentially, you need to give the “network service” account permissions to do anything that you want ASP.NET to be able to do. If you want to be able to delete files, give the Network Service account permissions to delete files in the appropriate folder.

In the next couple of weeks, I’m hoping to create a C#/ASP.NET based file-manager. It’s very likely that we’ll be using a lot of these methods for this project. If there are any features you’d like to see developed in it, don’t hesitate to contact me.

Update – Mar 18 @ 9:00 PM – When I wrote the above commands, I initially neglected to use escape sequences for the file names. Remember to always use a double back-slash or start your string with the @ symbol for your physical file paths to work properly. I’ve updated the commands to reflect the correct way to reference directories.

Comments

  1. hopefully your teaching your students that in C#, you need to use either a @ in front of a filename path or use \\

  2. Fair criticism. We didn’t talk a ton about IO in CIS-375, just enough to upload files, but I made sure they used escape sequences (or “\\”). I’ll have to go back and put them in the post. Thanks!

Speak Your Mind

*