Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to interact with File; File System

4.80/5 (5 votes)
23 Oct 2014MIT3 min read 9.9K  
How to interact with file; file system

Every website on our Internet uses a File System, where the user sometimes gets access to write the data, as everyone has some access to read the data. Let's get the common examples.

For beginners: What do you think read/write permissions are?

When the user is being provided read permission, it means he can read all the files; for example, he can view all the text files (.html, .css, .js, etc) which are required for a user to visit and surf the website. Let’s say Error Code 403 is one of these, any error code that starts with a 4 (403, 401) is an error; which means the users will never get access to these files or directories until or unless the webmaster allows him/her!

Now, let's cover the write permission. This permission is granted to the users who can edit, update, or upload new data to the server’s file System. For example, you might use some service which lets you share your images or text data (For example, Facebook, Google+, Tumblr., WordPress, etc.). Now these websites allow their users to read (you know what read means now) and write some data (write = upload, update, delete, and other of this kind) to the server. This can be done on a database. Which we will cover in future, but for now!

Read: To read the files present on the server, where we can easily access the files by just giving a virtual link to the file.

Write: To let the users alter the current file System data. To let him upload, or delete the files present on the File System of the server.

Coming to the Point

When you select a file in the form, the basic idea for them is to provide you with a beautiful form, where you are asked to select a file, which indirectly is a

HTML
<input type="file" name="image" />

Which is used to select the files and then send it to the server, You will need enctype to convert and encode the data to the type which is best for image and other data upload. Such as...

HTML
<form method="post" enctype="multipart/form-data">

...and so on.

How to Access and Work with File System

In ASP.NET, it's really simple to access and update the files present on the server’s file system. All you need is just a few variables to make all the process simple and user readable.

You remember, how we interact with the form submission by setting their methods to post. We will be using the same method here!

HTML
<form method="post">
   <input type="text" name="name" />
   <input type="submit" value="Submit" />
</form>

Now, when this is submitted, all the data (input, select, textarea) is submitted to the server along with other data that is inside the current form only.

Now let's handle this data on the server. We will start the IsPost block and read the data from the form and do some stuff in the server.

I need you to do something before that:

  1. Create a folder somewhere, let's say C:<username>\Documents\filename.txt
  2. Now, you will use this link, and write the stuff!
C#
if (IsPost) {
   var name = Request["FirstName"];
   var Data = name + Environment.NewLine; // new line there in the file..
   var FileToSaveOn = Server.MapPath("write the link to the file that you created here..");
        File.WriteAllText(@FileToSaveOn, Data);
}

Now go to the file in the document of yours. You will see the data written in the file.

Retry the whole process by changing the variable name to understand how they interact, try to change the code to get the information about how you can change and update the code and make it useful. There are some other types of the file property too. You can use them to delete, update or create files too! Example are:

C#
File.AppendAllText(); // this will update the file

There is just a slight difference between AppendAllText and WriteAllText, and it's that WriteAllText which will remove the previous data and then write the current data only. AppendAllText will keep the previous data and add this data at the end of the file. Pretty nice, huh? :)

C#
File.Exists("link to the file"); // checks whether the file is available or not..
File.ReadAllLines(); // reads the data in the file..
File.Delete(); // deletes the file; whose link is in paranthesis..

It's a better approach to first check whether file is present or not before proceeding, because it might give you an Exception, and for a developer Exceptions ain’t good! You should handle these tiny exceptions by using null as value inside the if else block.

In my next post, I will cover the basics of the file uploading and saving.

License

This article, along with any associated source code and files, is licensed under The MIT License