Click here to Skip to main content
13,254,366 members (55,902 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 23 Oct 2014

How to interact with File; File System

, 23 Oct 2014
Rate this:
Please Sign up or sign in to vote.
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

<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, 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!

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

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!
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:

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? :)

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.


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


About the Author

Afzaal Ahmad Zeeshan
Pakistan Pakistan
Computer programmer, author, student, in a relationship and not available!

You may also be interested in...


Comments and Discussions

GeneralMy Vote 5 Pin
Shemeemsha29-Oct-14 21:09
memberShemeemsha29-Oct-14 21:09 
GeneralRe: My Vote 5 Pin
Afzaal Ahmad Zeeshan30-Oct-14 5:22
professionalAfzaal Ahmad Zeeshan30-Oct-14 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 23 Oct 2014
Article Copyright 2014 by Afzaal Ahmad Zeeshan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid