C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

How Get Information About A File


Posted Date: 03 Jan 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Nikhil AgarwalMember Level: Gold    
Rating: 1 out of 5Points: 10



Below code presents the FileSize class, which allows you to get information about a file. This program takes a single filename and displays the size and key dates regarding it.

// FileSize.cs -
//-----------------------------------------------
using System;
using System.IO;

class FileSize
{
public static void Main()
{
string[] CLA = Environment.GetCommandLineArgs();

FileInfo fiExe = new FileInfo(CLA[0]);

if ( CLA.Length < 2 )
{
Console.WriteLine("Format: {0} filename", fiExe.Name);
}
else
{
try
{
FileInfo fiFile = new FileInfo(CLA[1]);

if(fiFile.Exists)
{
Console.WriteLine("===================================");
Console.WriteLine("{0} - {1}",
fiFile.Name, fiFile.Length );
Console.WriteLine("===================================");
Console.WriteLine("Last Access: {0}",
fiFile.LastAccessTime);
Console.WriteLine("Last Write: {0}",
fiFile.LastWriteTime);
Console.WriteLine("Creation: {0}",
fiFile.CreationTime);
Console.WriteLine("===================================");
}
else
{
Console.WriteLine("{0} doesn't exist!", fiFile.Name);
}
}

catch (System.IO.FileNotFoundException)
{
Console.WriteLine("\n{0} does not exist!", CLA[1]);
return;
}
catch (Exception e)
{
Console.WriteLine("\nException thrown trying to copy file.");
Console.WriteLine(e);
return;
}
}
}
}


The following is the output from this listing. Your output will vary depending on when you create the FileSize.cs program.

===================================
FileSize.cs - 1551
===================================
Last Access: 5/22/2005 11:50:30 PM
Last Write: 5/22/2005 11:50:19 PM
Creation: 5/22/2005 11:49:45 PM

The FileInfo class creates an object that is associated to a specific file. In line 12, a FileInfo object was created called fiExe that is associated to the program being executed (fileinfo.exe). If the user doesn't enter an argument on the command line, the value of fiExe is printed with program usage information (line 14).
In line 22, a second FileInfo object is created using the argument passed to the program. In lines 26 to 32, information is displayed about this file. As you can see, the information displayed includes the file size using the Length member, the creation date and time using the CreationTime member, the last access date and time using the LastAccessTime member, and finally the last date and time the file was written to using the LastWriteTime member.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
How Get Information About A File  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: File Upload
Previous Resource: Function For Password-Checking
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use