您的位置:首页 > 编程语言 > C#

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

2010-04-23 11:34 288 查看
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. }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: