Get the file Information using C#

No.of Views2314
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : CSharp , Miscellaneous
Get the file Information using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This Article explain some useful functinality in System.IO.FileInfo class. and also few useful method for the file size customization.

Implementation

The Windows system could store additional information like provide additional inforamtion with fileInfo class and more and more. and base dll System.dll.

Then we need design a simple GUI like followings.

GUI

It's simple user interface for this demo,and also source code can be download bellow link

Using Code


{codecitation class="brush: csharp; gutter: true;" width="650px"}

#region [ string GetSegment (string ParentPath)]
///
/// Get File segment and size.
///

///
///
public string GetSegment (string ParentPath)
{

string Container = string.Empty;
int segmentCount = 0;
long lastsegmentSize = 0;

long fileSizeinBytes = 0;
try
{
if (!string.IsNullOrEmpty (ParentPath))
{
if (File.Exists (ParentPath))
{
FileInfo objRecordedFileInfo = new FileInfo (ParentPath);
fileSizeinBytes = objRecordedFileInfo.Length;

if (fileSizeinBytes > Convert.ToInt64 (bufferSize))
{
segmentCount = Convert.ToInt32 (fileSizeinBytes / bufferSize);
lastsegmentSize = Convert.ToInt64 (fileSizeinBytes % bufferSize);
Container = segmentCount + "|" + lastsegmentSize + "|" + fileSizeinBytes;
}
else
{
Container = segmentCount + "|" + fileSizeinBytes + fileSizeinBytes;
}
}
else
{
Container = segmentCount + "|" + lastsegmentSize + fileSizeinBytes;
}
}
}
#region [ Exception Handling in Presetation Layer ]

#region [General Exception ]
catch (Exception ex)
{
throw ex;
}
#endregion

#endregion

return Container;
}
#endregion

{/codecitation}

and also you can customize buffer size.

and another simple snippt for File basic attribue provide by the Microsoft class library,but here demo

Rsult

and snippts

{codecitation class="brush: csharp; gutter: true;" width="650px"}

private void GetFileAttibutes (string Filename)
{
FileInfo objFileInfo=new FileInfo(Filename);
FileAttributes objFileAttributes = objFileInfo.Attributes;
switch (objFileAttributes)
{
case FileAttributes.Archive:
chkArchive.Checked = true;
break;
case FileAttributes.Compressed:
chkCompressed.Checked = true;
break;
case FileAttributes.Device:
chkDevice.Checked = true;
break;
case FileAttributes.Directory:
chkDirectory.Checked = true;
break;
case FileAttributes.Encrypted:
chkEncrypted.Checked = true;
break;
case FileAttributes.Hidden:
chkHidden.Checked = true;
break;
case FileAttributes.Normal:
chkNormal.Checked = true;
break;
case FileAttributes.NotContentIndexed:
chkNotContentIndexed.Checked = true;
break;
case FileAttributes.Offline:
chkOffline.Checked = true;
break;
case FileAttributes.ReadOnly:
chkReadOnly.Checked = true;
break;
case FileAttributes.System:
chkSystem.Checked = true;
break;
case FileAttributes.Temporary:
chkTemporary.Checked = true;
break;

}
}

{/codecitation}

Conclusion

This is article explain about how to get a fille information using .NET classes.

Thank you

RRaveen

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top