enumeratefile.cs

来自「微软的行业应用解决方案示例」· CS 代码 · 共 82 行

CS
82
字号
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;


namespace Microsoft.Mobile.Data
{
    /// <summary>
    /// Enumerate a file as a single column row.
    /// </summary>
    [SuppressMessage("Microsoft.Naming", 
                     "CA1710:IdentifiersShouldHaveCorrectSuffix",
                     Justification="More readable as EnumerateFile")]
    public class EnumerateFile : RowEnumerator
    {
        #region Fields
        String _fileName;
        StreamReader _stream;
        #endregion


        #region Constructor(s) & Dispose
        /// <summary>
        /// Build an enumerator using the filename as what file to open and 
        /// read. The file contents will be read a single line at a time and 
        /// put into a single column row.
        /// </summary>
        /// <param name="fileName">Filename including the path to open</param>
        public EnumerateFile(String fileName)
        {
            _fileName = fileName;
            _stream = new StreamReader(fileName);
        }


        /// <summary>
        /// Close the open stream and dispose of it.
        /// </summary>
        protected override void  Dispose(bool disposing)
        {
            _stream.Close();
            _stream.Dispose();
        }
        #endregion


        #region Methods
        /// <summary>
        /// Read in the next line of the file. If a line exist then build a 
        /// row otherwise return false to denote the end of the file.
        /// </summary>
        /// <returns></returns>
        public override bool MoveNext()
        {
            bool result = false;
            if (!_stream.EndOfStream)
            {
                currentRow = new DataRow();
                currentRow.Add("FileValue", _stream.ReadLine());
                result = true;
            }
            return result;
        }


        /// <summary>
        /// Close the current stream reading the file and re-open the file to 
        /// start reading the file again.
        /// </summary>
        public override void Reset()
        {
            _stream.Close();
            _stream.Dispose();
            _stream = new StreamReader(_fileName);
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?