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. Lets 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 untill or unless webmaster allows him/her!

Now, lets cover the write permission. This permission is granted to the users which 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 beautifull form, where you are asked to select a file, which indirectly is a

<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

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

And so on.

How to access and work with file System

In ASP.NET, its 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!

<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 alongwith other data that is inside the current form only.

Now lets 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, lets say C:\<username>\Documents\filename.txt
  2. Now, you will use this link, and write the stuff!
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 Documents 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 usefull. There are some other types of the File property too. You can use them to delete, update or create files too! Example are:

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

There is just a slight difference between AppendAllText and WriteAllText, and its that WriteAllText 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? 🙂

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..

Its 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s