I am going to start a new series of posts regarding files and how we can manipulate them in our ASP.Net applications. I will also present samples of .Net code that will exhibit how to get information on drives and folders.
I am not going to argue that storing our important data in databases is the way to go. I know that most people want to learn about ADO.Net & ASP.Net but in these series of articles,I will be writing about the main FCL classes that allow us to manipulate files&folders from our ASP.Net applications. Traditional file access is generally much less useful in a web application than it is in a desktop program.
Databases, on the other hand, are designed to support a large load of simultaneous users with speed, safety, and efficiency.File access is so easy and straightforward in .NET that it may be perfect for simple,small-scale solutions that don’t need a full external database.
Before I go on with my actual hands on examples I want to highlight the various limitations when it comes to files.
The first problem is that we have file naming limitations. Every file must have a unique name and there is no an easy way to ensure that a file's name is unique.
In the database world of course this very easy ( to have unique records ). I have seen people creating a file name based on a random number and the current date.
We do have more limitations when it comes to multiusers.In the ADO.Net world we do have an abundance of features that help us manage locking,inconsistent data and transactions.
The problem with files is not when we have multiple users reading from a file but when we want to have multiple users updating a file at the same time. That is almost impossibleto do without some major catastrophe. Of course scalability is also an issue. Our ASP.Net application can work fine when connecting and reading data from a file with 10 users but not with 300.
Before we start with our hands-on example it would be great if you could have a look at the System.IO namespace and its classes.The System.IO
namespace within the .NET Framework makes working with file system directories and files very easy.
Our first example would be on how to display local drive information.Keep in mind that because your ASP.NET applications are executing on the server, the file system you are accessing is the one your Web application
is running on. You, of course, cannot use an ASP.NET application to access the end user’s file system.
We want to find the following information
|Drive Name|| |
|Drive Type|| |
|Available Free Space||
|Drive Format|| |
I will use Visual Studio 2010 Ultimate edition and VB.Net as the development language.
1) Create an ASP.Net site and name it as you like
2) Add a new page in your web site. Name it DriverInfo.aspx. In the markup of the page add this snippet
<asp:Label ID="lblDriveName" runat="server" Text="Label" />
<asp:Label ID="lblDriveType" runat="server" Text="Label"/>
<tr><td>Available Free Space:</td><td>
<asp:Label ID="lblAvailableFreeSpace" runat="server" Text="Label" />
<asp:Label ID="lblDriveFormat" runat="server" Text="Label" />
<asp:Label ID="lblTotalSize" runat="server" Text="Label" />
3) We just add some label controls on the .aspx page
4) At the beginning of the DriverInfo.aspx.vb page add this line of code
5) In the Page_Load event handling routine of the DriverInfo.aspx.vb page, add this VB.net code
Dim drive As New DriveInfo("C:\")
lblDriveName.Text = drive.Name
lblDriveType.Text = drive.DriveType.ToString()
lblAvailableFreeSpace.Text = drive.AvailableFreeSpace.ToString()
lblDriveFormat.Text = drive.DriveFormat
lblTotalSize.Text = drive.TotalSize.ToString()
6) We have used a class introduced in the .Net 3.5 class library , the DriverInfo class. This class supplements the Get-LogicalDrives() method of the Directory class included in prior versions of the .NET Framework. In the code above we just use an object of type DriveInfo and its properties to get the required information back.
7) Run your application and see how your page looks like when you view it in a browser.
I will post more posts regarding files & folders in ASP.Net. I will keep these posts short do we only focus on a specific issue at a time.
Hope it helps!!!