Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Save and read file from server in Asp.Net using .Net 2.0.

Author: Zunnair
Download Source Code :

In this article I will try to explain, how you can save and read file from server i.e. ‘wwwroot’ and access file attributes that resides on server in ASP.Net

The technique for doing the above mentioned tasks is very simple all you have to do is create a new website in VS and add the Label control, File Upload control, Text Box control and finally the Button Control on your Default.aspx page. The code for your web form will be look like as follows:



After adding controls, now add the code on cmdShowContents event:





Protected Sub cmdShowContents_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdShowContents.Click


ViewState("fileName") = fuUploadFile.FileName

 If Not fuUploadFile.PostedFile Is Nothing Then

 fuUploadFile.PostedFile.SaveAs("C:\Inetpub\wwwroot\ReadAllTextPro\" & fuUploadFile.FileName)

 txtDisplayFile.Text = File.ReadAllText(Server.MapPath(fuUploadFile.FileName))

 End If

End Sub


protected void cmdShowContents_Click(object sender, System.EventArgs e) {

    ViewState("fileName") = fuUploadFile.FileName;

    if ((fuUploadFile.PostedFile != null)) {

        fuUploadFile.PostedFile.SaveAs("C:\\Inetpub\\wwwroot\\ReadAllTextPro\\" + fuUploadFile.FileName);

        txtDisplayFile.Text = File.ReadAllText(Server.MapPath  (fuUploadFile.FileName));



Notice that I have used the ViewState. The reason is I need to maintain the value of read file at multiple post backs. Notice one more thing here I have used the ‘ReadAllText’ method that returns the file in form of string. You can perform the similar task by using the StreamReader. But this will not read file from server it reads from buffer. To read file from server you always need to save the file in ‘wwwroot’ and then access. Remember one thing, to work with File Class you need to import the System.IO namespace. Now add the code in cmdViewFilePro event: 




Protected Sub cmdViewFilePro_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdViewFilePro.Click


 Dim fn As String = ViewState("fileName")

 Dim FilePro As New System.IO.FileInfo(Server.MapPath(fn))

 Dim nString As New StringBuilder


 nString.Append("FileName : " & FilePro.FullName)

 nString.Append("Size : " & FilePro.Length)

 nString.Append("Created : " & FilePro.CreationTime)

 nString.Append("Modified : " & FilePro.LastWriteTime)

 nString.Append("Access : " & FilePro.LastAccessTime)

 nString.Append("Attribute : " & FilePro.Attributes)

 nString.Append("ReadOnly : " & FilePro.IsReadOnly)

 nString.Append("FileName : " & FilePro.Extension)

 lblShowPro.Visible = True

 lblShowPro.Text = nString.ToString

End Sub



  1. protected void cmdViewFilePro_Click(object sender, System.EventArgs e)
  2. {
  3. string fn = ViewState("fileName");
  5.     System.IO.FileInfo FilePro = new System.IO.FileInfo(Server.MapPath(fn));
  6.      StringBuilder nString = new StringBuilder();
  7.      nString.Append("FileName : " + FilePro.FullName);
  8.      nString.Append("Size : " + FilePro.Length);
  9.      nString.Append("Created : " + FilePro.CreationTime);
  10.      nString.Append("Modified : " + FilePro.LastWriteTime);
  11.      nString.Append("Access : " + FilePro.LastAccessTime);
  12.      nString.Append("Attribute : " + FilePro.Attributes);
  13.      nString.Append("ReadOnly : " + FilePro.IsReadOnly);
  14.      nString.Append("FileName : " + FilePro.Extension);
  15.      lblShowPro.Visible = true;
  16.      lblShowPro.Text = nString.ToString;
  17.  }


Here I have accessed the file name from the ViewState and then Map its path and finally get the file attributes and display on my browser


This is a simple article .i hope u will like and enjoying this Article. If you feel any difficulty then reference the source code.because Source code is given with this Article for your proper understanding.


Add Article Comment:
Name :
Email Address :
Comments :
<< Creating User Controls without Interface

Disclaimer - Privacy
© 2002-2017