Friday, June 12, 2009

How To Read File Information With ASP.NET?

If you try to create file manager in ASP.NET or simply you want to know some specific attribute of some file, you can use FileInfo class to get all needed file information. Bellow is simple code example about how to use FileInfo class. You can copy/paste this code to see how it works. Just replace "products.xml" with needed file.

[ C# ]

// To read file information, we need System.IO namespace
using System.IO;

public partial class DefaultCS : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
string FilePath = Server.MapPath("products.xml");
FileInfo MyFileInfo = new FileInfo(FilePath);
string FileInformation = "File information:
";
// Check if file exists
if(MyFileInfo.Exists)
{
// File exists, take data about it
FileInformation = "File Name: " + MyFileInfo.Name + "
";
FileInformation += "Directory: " + MyFileInfo.DirectoryName + "
";
FileInformation += "Full path: " + MyFileInfo.FullName + "
";
FileInformation += "Time Created: " + MyFileInfo.CreationTime + "
";
FileInformation += "Time Modified: " + MyFileInfo.LastWriteTime + "
";
FileInformation += "Last Access Time: " + MyFileInfo.LastAccessTime + "
";
// Gets file size in bytes
FileInformation += "File Size: " + MyFileInfo.Length + "
";
// Gets extension part of file name
FileInformation += "File Extension: " + MyFileInfo.Extension;
}
else
{
// File not exists, inform a user about it
FileInformation = "There is no file on " + FilePath + ".";
}
Response.Write(FileInformation);
}
}

[ VB.NET ]

' To read file information, we need System.IO namespace
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim FilePath As String = Server.MapPath("products.xml")
Dim MyFileInfo As FileInfo = New FileInfo(FilePath)
Dim FileInformation As String = "File information:
"
' Check if file exists
If (MyFileInfo.Exists) Then
' File exists, take data about it
FileInformation = "File Name: " & MyFileInfo.Name & "
"
FileInformation &= "Directory: " & MyFileInfo.DirectoryName & "
"
FileInformation &= "Full path: " & MyFileInfo.FullName & "
"
FileInformation &= "Time Created: " & MyFileInfo.CreationTime & "
"
FileInformation &= "Time Modified: " & MyFileInfo.LastWriteTime & "
"
FileInformation &= "Last Access Time: " & MyFileInfo.LastAccessTime & "
"
' Gets file size in bytes
FileInformation &= "File Size: " & MyFileInfo.Length & "
"
' Gets extension part of file name
FileInformation &= "File Extension: " & MyFileInfo.Extension
Else
' File not exists, inform a user about it
FileInformation = "There is no file on " & FilePath & "."
End If
Response.Write(FileInformation)
End Sub
End Class

http://www.beansoftware.com/ASP.NET-FAQ/Read-File-Information.aspx
--------------------------------------------------------------------------
Get FileInfo: file size, file attribute list
using System;
using System.IO;

class MainClass
{
public static void Main(string[] args)
{
FileInfo file = new FileInfo("c:\\test.txt");

Console.WriteLine("Checking file: " + file.Name);
Console.WriteLine("File exists: " + file.Exists.ToString());

if (file.Exists)
{
Console.Write("File size (bytes): ");
Console.WriteLine(file.Length.ToString());
Console.Write("File attribute list: ");
Console.WriteLine(file.Attributes.ToString());
}

}
}

No comments: