简单的C# dat文件读取方法

来源:互联网 发布:nginx 加 https 编辑:程序博客网 时间:2024/05/16 06:31

string filepath = @"C:/test.dat";
        string[] data = File.ReadAllLines(filepath, Encoding.Default);
        foreach (string line in data)
        {
            string a = line;
        }

 

来自朋友的一个测试小例子

 

以下为其他网页转载:ASP.NET做的web,服务端把一个文件转换成了Byte[]类型的字节数组返回,在客户端接这个数据啊,然后把这个文件保存下来

 

  /// <summary>
    /// 读取路径下的文件并保存为新文件
    /// </summary>
    /// <param name="filePath"></param>
    public static void DownLoadFile(string filePath)
    {
        HttpResponse rsp = HttpContext.Current.Response;

        if (filePath.StartsWith("~/"))
        {
            filePath = HttpContext.Current.Server.MapPath(filePath);
        }

        FileStream f = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

        rsp.ClearHeaders();
        rsp.ContentType = "application/x-msdownload";
        rsp.AddHeader("Content-Disposition", "attachment;filename= " + Path.GetFileName(filePath));
        rsp.AddHeader("Content-Length", f.Length.ToString());

        byte[] buffer = new byte[65536]; //ζ棬
       // byte[] mFileByte = new Byte[fileSize];

        while (true)
        {
            int b = f.Read(buffer, 0, buffer.Length);


            if (b == 0) break;
            rsp.BinaryWrite(buffer);
         
        }
        f.Close();
        rsp.Flush();
        rsp.Close();
    }


/******************************************************************/

 

 

dat文件数据读到文本框

using System.IO;

1.             StreamReader objInput = new StreamReader("C://values.dat", System.Text.Encoding.Default);

2.             string contents = objInput.ReadToEnd().Trim();

3.             string [] split = System.Text.RegularExpressions.Regex.Split(contents, "//s+", RegexOptions.None);             

4.             foreach (string s in split)

5.             {

              // 注意转换数据类型,否则为一堆乱码

6.                       Console.WriteLine(s);

//  TextBox1.Text =s+i;

7.             //            i = s + i;

8.             }

 

原创粉丝点击