65.9K
CodeProject 正在变化。 阅读更多。
Home

本文主要介绍.NET 2.0 IO 基类库中的文件属性

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.48/5 (9投票s)

2007 年 2 月 26 日

CPOL

2分钟阅读

viewsIcon

21235

downloadIcon

179

本文介绍了一些有用的文件信息和基本文件属性。

Screenshot - FM1.jpg

下载 FileManagement_demo.zip - 11.1 KB

下载 FileManagement_src.zip - 156 KB

引言

本文介绍 `System.IO.FileInfo` 类中一些有用的功能,以及用于文件大小自定义的几个有用方法。

背景

Windows 系统可以存储额外的 fileInfo 类信息,以及更多的信息。基础 DLL 为*System.dll*。

这个演示的 UI 很简单,源代码也可以通过下面的链接下载。

使用代码

代码片段

<code>

#region [ string GetSegment (string ParentPath)]
/// <summary>
/// 获取文件分段和大小。
/// </summary>
/// <param name="ParentPath"></param>
/// <returns></returns>
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 [ Presentation Layer 中的异常处理 ]

#region [通用异常 ]
catch (Exception ex)
{
throw ex;
}
#endregion

#endregion

return Container;
}
#endregion

</code>

您还可以自定义 bufferSize。

这是 Microsoft 类库提供的另一个简单的文件基本属性代码片段,这里是演示。

Screenshot - FM2.jpg

代码片段

<code>

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;

}
}

</code>

谢谢大家

另外,这是我的第一篇文章,可能在分类上有疏漏,请通过电子邮件提出您的建议。

谢谢 祝您愉快!!!

© . All rights reserved.