简单的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. }
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. }
相关文章推荐
- C#简单读取、改变文件的创建、修改及访问时间的方法
- c#简单读取文本的实例方法
- C#简单读取主机上所有进程的方法
- 一种c#读取文本比较简单的方法
- 【转载】C#连接数据库及读取数据库中字段的简单方法总结
- ASP.NET + C#读取ACCESS数据库内容的简单方法
- C#中读取XML到Dataset最简单的方法.
- C#程序连接数据库及读取数据库中字段的简单方法总结
- ASP.NET + C#读取ACCESS数据库内容的简单方法
- C#读取配置文件的方法汇总
- 在C#中读取Excel文件的两个方法
- ASP.NET(C#) 读取EXCEL另加解决日期问题的方法分享
- 读取连接字符串最简单的方法
- 一个简单的C#多线程间同步(事件方法)的例子
- 简单干净的C#方法设计案例:SFCUI.AjaxLoadPage()之二
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法 (转)
- C#读取二进制文件方法分析
- 在C#中创建和读取XML文件的实现方法
- 用C#读取txt文件的方法(转)
- kindeditor4.1.4 asp.net C# 配置及相关使用技巧 附带读取txt文本方法