asp.net 如何按行读取txt文件?

asp.net 如何按行读取txt文件?,第1张

直接用静态类file的ReadAllLines(string path)函数,他返回以行为单元的字符串数组string[] arr

第三行就是 arr[2] 一千行就是arr[999]

如果想从指定位链慧敬置读取

可以创建filestream对象,然后用seek()方法指定文件指针位置,基于此对象的棚慎streamreader就可以从指定位置碧燃读取数据。

方法一

using System

using System Data

using System Configuration

using System Collections

using System Web

using System Web Security

using System Web SessionState

using System Timers

using System Net

using System IO

using System Text

using System Threading

namespace

{

public class Global : System Web HttpApplication

{

protected void Application_Start(object sender EventArgs e)

{

//定义定时器

System Timers Timer myTimer = new System Timers Timer( )

myTimer Elapsed += new ElapsedEventHandler(myTimer_Elapsed)

myTimer Enabled = true

 晌察搜 myTimer AutoReset = true

}

void myTimer_Elapsed(object source ElapsedEventArgs e)

{

try

{

Log SaveNote(DateTime Now ToString( yyyy MM dd HH:mm:ss ) + :AutoTask is Working! )

YourTask()

}

catch (Exception ee)

{

Log SaveException(ee)

}

}

void YourTask()

{

//在这里写你需要执行的任务

}

protected void Application_End(object sender EventArgs e)

{

Log SaveNote(DateTime Now ToString( yyyy MM dd HH:mm:ss ) + :Application End! )

//下面的代码是关键 可解决IIS应用程序池自动回收的问题

Thread Sleep( )

//这里设置你的web地址 可以随便指向你的任意一个aspx页面甚至不存在的页面 目的是要激发Application_Start

string url =

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest Create(url)

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest GetResponse()

Stream receiveStream = myHttpWebResponse GetResponseStream()//得到回写的字节流

}

}

}

原理 Global asax 可以是中应用程序或会话事件处理程序 我们用到宴历了Application_Start(应用程序开始事件)和Application_End(应用程序结束事件) 当应用程序开始时 启动一个定时器 用来定时执行任务YourTask()方法 这个方法里面可以写上需要调用的逻辑代码 可没伍以是单线程和多线程 当应用程序结束时 如IIS的应用程序池回收 让去访问当前的这个web地址 这里需要访问一个aspx页面 这样就可以重新激活应用程序 Log类是一个记录日志的一个类 下面是测试时生成的日志信息

================================================================

: : :AutoTask is Working!

: : :AutoTask is Working!

: : :AutoTask is Working!

: : :Application End!

: : :AutoTask is Working!

: : :AutoTask is Working!

从日志中发现 当手动回收IIS的应用程序池之后 计划任务还在执行 说明我们的目的达到了

如果将Application_End中的代码注释掉 会发现Application End之后 计划任务停止工作了 如下

================================================================

: : :AutoTask is Working!

: : :AutoTask is Working!

: : :AutoTask is Working!

: : :Application End!

局限性 可以解决应用程序池自动或者手动回收 但是无法解决IIS重启或者web服务器重启的问题 当然这种情况出现的时候不多 而且如果有人访问你的网站的时候 又会自动激活计划任务了

方案二

<%@ Application Language= C# %>

<%@ import Namespace= System IO %>

<script runat= server >

void Application_Start(object sender EventArgs e)

{

// 在应用程序启动时运行的代码

System Timers Timer myTimer = new System Timers Timer( )

myTimer Elapsed += new System Timers ElapsedEventHandler(OnTimedEvent)

myTimer Interval =

myTimer Enabled = true

}

void Application_End(object sender EventArgs e)

{

//  在应用程序关闭时运行的代码

}

void Application_Error(object sender EventArgs e)

{

// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender EventArgs e)

{

// 在新会话启动时运行的代码

}

void Session_End(object sender EventArgs e)

{

// 在会话结束时运行的代码

// 注意: 只有在 nfig 文件中的 sessionstate 模式设置为

// InProc 时 才会引发 Session_End 事件 如果会话模式设置为 StateServer

// 或 SQLServer 则不会引发该事件

}

private static void OnTimedEvent(object source System Timers ElapsedEventArgs e)

{

//间隔时间执行某动作

//指定日志文件的目录

string fileLogPath = AppDomain CurrentDomain BaseDirectory + SystemLog

string fileLogName = SoftPrj_CN_ + DateTime Now ToLongDateString() + _log txt

//定义文件信息对象

FileInfo finfo = new FileInfo(fileLogPath + fileLogName)

//创建只写文件流

using (FileStream fs = finfo OpenWrite())

{

//根据上面创建的文件流创建写数据流

StreamWriter strwriter = new StreamWriter(fs)

//设置写数据流的起始位置为文件流的末尾

strwriter BaseStream Seek( SeekOrigin End)

//写入错误发生时间

strwriter WriteLine( 发生时间: + DateTime Now ToString())

//写入日志内容并换行

//strwriter WriteLine( 错误内容: + message)

strwriter WriteLine( 错误内容: )

//写入间隔符

strwriter WriteLine( )

strwriter WriteLine()

//清空缓冲区内容 并把缓冲区内容写入基础流

strwriter Flush()

//关闭写数据流

strwriter Close()

fs Close()

}

}

</script>

方案三

<%@ Application Language= C# %>

<%@ Import Namespace= System IO %>

<%@ Import Namespace= System Threading %>

<script RunAt= server >

string LogPath

Thread thread

void WriteLog()

{

while (true)

{

StreamWriter sw = new StreamWriter(LogPath true Encoding UTF )

sw WriteLine(thread Name + : + DateTime Now ToString())

sw Close()

Thread CurrentThread Join( * )//阻止 秒

}

}

void Application_Start(object sender EventArgs e)

{

LogPath = HttpContext Current Server MapPath( log txt )        //在应用程序启动时运行的代码

thread = new Thread(new ThreadStart(WriteLog))

thread Name = 写登录日志线程

thread Start()

}

void Application_End(object sender EventArgs e)

{

//  在应用程序关闭时运行的代码

}

void Application_Error(object sender EventArgs e)

{

// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender EventArgs e)

{

// 在新会话启动时运行的代码

}

void Session_End(object sender EventArgs e)

{

// 在会话结束时运行的代码

// 注意: 只有在 nfig 文件中的 sessionstate 模式设置为

// InProc 时 才会引发 Session_End 事件 如果会话模式设置为 StateServer

// 或 SQLServer 则不会引发该事件

}

lishixinzhi/Article/program/net/201311/13724

这里贴的代码比较乱,可以看参考资料中的连接!

效果:比如在第4行渗槐后面插入“我是阿会楠”,则结果为:

1

2

3

4

我是阿会楠

5

6

7

8

9

目录下必须有“7.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)

{

string Path = Server.MapPath("7.txt")

string str = "我是阿会楠"

insertStr(Path,str,4)

}

public void insertStr(string Path,string str,int n)

{

string strLineStart = ""

string strLineEnd = ""

string strLineEnd2 = ""

string strLine = ""

try

{

//实例化一个StreamReader对象,并申明编码为GB2312

StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"))

//读丛漏友取插入前的数据

for (int i = 0i <ni++)

{

strLineStart += sr.ReadLine() + "\r\n"

}

//后面的数据

while (strLineEnd != null)

{

strLineEnd = sr.ReadLine()

strLineEnd2 += strLineEnd + "\r\n"

}

strLine = strLineStart + str + "\r\n" + strLineEnd2

//关闭

sr.Dispose()

sr.Close()

StreamWriter sw = new StreamWriter(Path, false, Encoding.GetEncoding("GB2312"))

sw.WriteLine(strLine)

//关闭

sw.Flush()

sw.Dispose()

sw.Close()

}

catch

{

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12301714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存