当前位置:编程学习 > C#/ASP.NET >>

如何把C#中的内存流 存放到一个TXT文档中,并且可以也可以读出数据,显示在一个txt文本框中

如题,求上面的一段代码,谢谢了。 --------------------编程问答-------------------- 用streamreader读出来,然后转换为字符串 --------------------编程问答--------------------

 public static class StreamHelper
    {
        public static Encoding GetEncoding(Stream stream, Encoding defaultEncoding)
        {
            Encoding targetEncoding = defaultEncoding;

            if (stream == null) return targetEncoding;
            if (stream.Length < 3) return targetEncoding;

            byte byte1, byte2, byte3;

            //保存当前Seek位置
            long origPos = stream.Seek(0, SeekOrigin.Begin);
            byte1 = Convert.ToByte(stream.ReadByte());
            byte2 = Convert.ToByte(stream.ReadByte());
            byte3 = Convert.ToByte(stream.ReadByte());

            //根据文件流的前3个字节判断Encoding          
            if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
            {
                targetEncoding = Encoding.BigEndianUnicode;
            }

            if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
            {
                targetEncoding = Encoding.Unicode;
            }

            if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
            {
                targetEncoding = Encoding.UTF8;
            }

            //恢复Seek位置
            stream.Seek(origPos, SeekOrigin.Begin);

            return targetEncoding;
        }
    } 


 private void InitComboBox()
        {
            List<string> ServerNames = new List<string>();
            ServerNames = this.LoadName();
            this.cmbServer.Items.AddRange(ServerNames.ToArray());
            this.cmbCategory.Items.AddRange(new string[] { "Error", "Exception", "Info" });
        }

        private void SetSavePath(ref string savePath)
        {
            FolderBrowserDialog FolderPath = new FolderBrowserDialog();
            FolderPath.ShowDialog();
            savePath = FolderPath.SelectedPath;
        }

 private void btnDownloadBatch_Click(object sender, EventArgs e)
        {
            DateTime dt = this.dtpStart.Value;
            string savePath = String.Empty;

            Server = this.cmbServer.Text;
            Path = this.txtPath.Text + @"\Log\" + cmbCategory.Text;
            if (!Directory.Exists(Path)) { MessageBox.Show("路径不存在"); return; }
            SetSavePath(ref savePath);

            //下面那两句防止间隔小于24小时但跨越两天才算一天的情况
            DateTime start = new DateTime(this.dtpStart.Value.Year, this.dtpStart.Value.Month, this.dtpStart.Value.Day, 0, 0, 0);
            DateTime end =  new DateTime(this.dtpEnd.Value.Year, this.dtpEnd.Value.Month, this.dtpEnd.Value.Day, 23, 59, 59);

            TimeSpan ts = end - start;
            double dDays = ts.TotalDays;//带小数的天数,比如1天12小时结果就是1.5 
            int DateCount = (int)Math.Ceiling(dDays);//返回有效数就进一的整数

            this.progressBar1.Value = 0;
            DownLoadBatch(dt, end, DateCount, savePath);          
        }
--------------------编程问答-------------------- 谢谢分享学习一下
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,