Lessons XIV : Code Example – một cái ví dụ lấy nội dung một file

mình mởi đầu một cái ví dụ lấy nội dung một file!

PHP Code:

using System;
using System.IO;
using System.Text;

/*
* Created by SharpDevelop.
* NetDevelop Co., Ltd.
* Author: Tuan Anh Nguyen Ngoc
* Date: 11/21/2006
* Contact Information.
* – Email: [email]info.netdevelop@gmail.com[/email]
* – Handheld: +84 905 202 088
*/
namespace Ans.Utilities
{

public class File
{
/// <summary>
/// define string fullpath.
/// </summary>
private string _sFullPath;
/// <summary>
/// Contruction of this class.
/// </summary>
public File() { }
/// <summary>
/// Contruction of this class.
/// </summary>
/// <param name=”_pfullpath”>The string fullpath.</param>
public File(string _pfullpath)
{
///set fullpath.
this.FullPath = _pfullpath;
}
/// <summary>
/// Get/Set fullpath string of file.
/// </summary>
public string FullPath
{
///return fullpath.
get { return this._sFullPath; }
///set fullpath.
set { this._sFullPath = value; }
}
/// <summary>
/// Get file content.
/// </summary>
/// <returns>return file content as string.</returns>
private string FileContent()
{
///define string result.
string _sResult =””;
///check file path. throw exception if is null or empty.
if(this.FullPath == null || this.FullPath.Equals(“”))
///throw exception.
throw new Exception(“Can not get content! The file is not define or declare.”);
else{
///check file exists, throw exception if it isn’t exist.
if (System.IO.File.Exists(this.FullPath))
{
///Create filestream with filemode open and fileaccess read.
FileStream _oFileStream = new FileStream(this.FullPath, FileMode.Open, FileAccess.Read);
///Create byte array.
Byte[] _oByte = new byte[1024];
///Create UTF8Encoding.
UTF8Encoding _oUTF8Encoding = new UTF8Encoding();
///while filestream read byte > 0.
while (_oFileStream.Read(_oByte, 0, _oByte.Length)>0)
{

_sResult += _oUTF8Encoding.GetString(_oByte);
}
}
else
///throw exception.
throw new Exception(“Can not get content! The file can not be found.”);
}
///return result.
return _sResult;
}
/// <summary>
/// Get file content.
/// orverload fileContent method.
/// </summary>
/// <param name=”_pfullpath”>The fullpath string.</param>
/// <returns>return file content as string.</returns>
public string FileContent(string _pfullpath)
{
///set fullpath.
this.FullPath = _pfullpath;
///call fileContent method, return result.
return FileContent();
}
}
}

__________________
dauden(UDS)

 

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: