c#文件操作

string fileName = this.FileUpload1.FileName;  //文件名
            string filePath = this.FileUpload1.PostedFile.FileName; //完整路径+文件名称
            string fileType = System.IO.Path.GetExtension(fileName);


            if (fileType == ".txt")
            {
                int fileLength = this.FileUpload1.PostedFile.ContentLength;
                byte[] input = new byte[fileLength];

                System.IO.Stream fileStream = this.FileUpload1.PostedFile.InputStream;
                fileStream.Read(input, 0, fileLength);
                fileStream.Position = 0;
                System.IO.StreamReader sr = new System.IO.StreamReader(fileStream, System.Text.Encoding.Default);

                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = sr.ReadLine();

                while (strLine != null)
                {
                    ListBox1.Items.Add(strLine);
                    strLine = sr.ReadLine();
                    //i++;
                } 
                
                //ListBox1.Items.Add(sr.ReadToEnd());
                fileStream.Close();
                sr.Close();

            }


你可能感兴趣的:(c#文件操作)