ASP.NET 读取TXT文档的值 RT 我现在要批量的往数据库添加数据,数据是从别的网站上复制下来的,想用TXT文档,求解怎么用.NET读取TXT文档 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 读取TXT文档并返回行数using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using System.Text;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string Path = Server.MapPath("4.txt"); insertStr(Path); } } public void insertStr(string Path) { string strLine = ""; int i = 0; try { StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312")); strLine = sr.ReadLine(); while (strLine != null) { Response.Write(strLine + "<br>"); strLine = sr.ReadLine(); i++; } Response.Write(i); sr.Dispose(); sr.Close(); } catch { } }} string ss = File.ReadAllText("D:\\1.txt"); )创建txt文件【web.config】 --------------------------------------------------------------------<appSettings> <add key="EditChars" value="D:\Site\ZJPS\TextFile\EditChars.txt"/></appSettings>2) 页面的CS文件中: --------------------------------------------------------------------【1】 获取txt文件位置: protected string FileName = System.Configuration.ConfigurationManager.AppSettings["EditChars"].ToString(); 【2】 文件操作:创建、写、读 文件。 #region 文件操作 /// <summary> /// 创建txt文件 /// </summary> public void CreateToFile() { StreamWriter SW; SW = File.CreateText(FileName); SW.Close(); } /// <summary> /// 写文件 /// </summary> public void WriteToFile() { string InsertStr = ""; if (!File.Exists(FileName)) { CreateToFile(); InsertStr = "Content#DateTime#!"; //txt文件中用它来表示字段 } InsertStr += this.txtContent.Text.ToString() + "#"; InsertStr += Convert.ToString(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")) + "#"; File.AppendAllText(FileName, InsertStr + "!", Encoding.BigEndianUnicode); } /// <summary> /// 读取文件 /// </summary> /// <returns></returns> public string ReadTextFileDate() { string strInput = ""; string GetStream = ""; if (File.Exists(FileName)) { StreamReader sr = new StreamReader(FileName, UnicodeEncoding.GetEncoding("gb2312")); strInput = sr.ReadLine(); while (strInput != null) { GetStream += strInput; strInput = sr.ReadLine(); } sr.Close(); } else { lblContents.Text = "myFile.txt does not exist!"; } return GetStream; } #endregion/// <summary> /// 修改txt中的内容/// </summary>public void UpdateTextFile(string FileName, string StrCount) { System.IO.StreamReader sr = new System.IO.StreamReader(FileName); string s = sr.ReadToEnd(); sr.Close(); s = s.Replace(s, StrCount); System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false); sw.Write(s); sw.Close(); } 请教 Ajax报错,找不到WEB服务 两个网站一个能访问一个不能访问 如何合并两个网站 求助!关于表连接查询? Frame 中用window.parent["Content"].location = "URL"; 刷新其中一页,能否在CS 代码中获得该页刷新前URL? server.Transfer 方法在Page_Load中好用,在button的click事件怎么就不好用了。 DropDownList1绑定排序问题、、 完全用.NET写成的论坛,大家给点意见 遇到过这种情况吗?? 急!按钮,关于层的隐藏和显示?? 急,配置文件的错误
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.IO;
using System.Text;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string Path = Server.MapPath("4.txt");
insertStr(Path);
}
} public void insertStr(string Path)
{
string strLine = "";
int i = 0;
try
{
StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312")); strLine = sr.ReadLine(); while (strLine != null)
{
Response.Write(strLine + "<br>");
strLine = sr.ReadLine();
i++;
}
Response.Write(i);
sr.Dispose();
sr.Close();
}
catch
{
}
}}
--------------------------------------------------------------------
<appSettings>
<add key="EditChars" value="D:\Site\ZJPS\TextFile\EditChars.txt"/>
</appSettings>
2) 页面的CS文件中:
--------------------------------------------------------------------
【1】 获取txt文件位置: protected string FileName = System.Configuration.ConfigurationManager.AppSettings["EditChars"].ToString();
【2】 文件操作:创建、写、读 文件。
#region 文件操作
/// <summary>
/// 创建txt文件
/// </summary>
public void CreateToFile()
{
StreamWriter SW;
SW = File.CreateText(FileName);
SW.Close();
}
/// <summary>
/// 写文件
/// </summary>
public void WriteToFile()
{
string InsertStr = "";
if (!File.Exists(FileName))
{
CreateToFile();
InsertStr = "Content#DateTime#!"; //txt文件中用它来表示字段
}
InsertStr += this.txtContent.Text.ToString() + "#";
InsertStr += Convert.ToString(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")) + "#";
File.AppendAllText(FileName, InsertStr + "!", Encoding.BigEndianUnicode);
}
/// <summary>
/// 读取文件
/// </summary>
/// <returns></returns>
public string ReadTextFileDate()
{
string strInput = "";
string GetStream = ""; if (File.Exists(FileName))
{
StreamReader sr = new StreamReader(FileName, UnicodeEncoding.GetEncoding("gb2312"));
strInput = sr.ReadLine();
while (strInput != null)
{
GetStream += strInput;
strInput = sr.ReadLine();
}
sr.Close();
}
else
{
lblContents.Text = "myFile.txt does not exist!";
}
return GetStream;
}
#endregion/// <summary> /// 修改txt中的内容/// </summary>public void UpdateTextFile(string FileName, string StrCount) { System.IO.StreamReader sr = new System.IO.StreamReader(FileName); string s = sr.ReadToEnd(); sr.Close();
s = s.Replace(s, StrCount);
System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false); sw.Write(s); sw.Close(); }