string temp;
file  = new  FileStream  ("file.txt",FileMode.Open) ;
reader  =  new  StreamReader(file);  
file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
temp= reader.ReadLine();
file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
temp=reader.ReadLine();结果:两次temp读的值不一样~~file.Seek根本没起作用````为什么呢?

解决方案 »

  1.   

    file.Seek  (  0  ,  SeekOrigin.End  )  ;
      

  2.   

    FileStream file;
                StreamReader reader;
                string temp;
                file  = new  FileStream("file.txt",FileMode.Open) ;
                reader  =  new  StreamReader(file);  
                file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
                temp= reader.ReadLine();
                file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
                temp=reader.ReadLine();我试了,一样的呀。你把你完整代码和file.txt文件内容贴出来看看。
      

  3.   

    源程序~~
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;namespace WindowsApplication26
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    FileStream file;
    StreamReader reader;
    string temp;
    file  = new  FileStream("file.txt",FileMode.Open) ;
    reader  =  new  StreamReader(file);  
    file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
    temp= reader.ReadLine();
    MessageBox.Show(temp);
    file.Seek  (  0  ,  SeekOrigin.Begin  )  ;
    temp=reader.ReadLine();
    MessageBox.Show(temp); }
    }
    }
      

  4.   

    试了一下,的确第二个file.seek好像没起什么作用。不知道是不是只能做一次file.seek操作。
      

  5.   

    file position after seek 1st time: 0
    file position after readline 1st time: 13
    temp: 1
    file position after seek 2nd time: 0
    file position after readline 2nd time: 0
    temp: 2起作用了file.
      

  6.   

    在第2个file.Seek  (  0  ,  SeekOrigin.Begin  )  ;之前(或之后)加一行:reader  =  new  StreamReader(file);就没问题了.