Tài liệu C Sharp part 13 pdf

3 365 0
Tài liệu C Sharp part 13 pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

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, Fi leAccess .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) . 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 leAccess .Read); ///Create byte array. Byte[] _oByte = new byte[1024]; ///Create UTF8Encoding. UTF8Encoding _oUTF8Encoding = new UTF8Encoding(); ///while

Ngày đăng: 15/12/2013, 01:16

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan