我做了一个每日一贴的功能,有前一贴和后一贴,我想按前一贴时往后读。。请问怎么实现
路径:
sr = new StreamReader(dir + "\\DOC\\每日一贴.txt",System.Text.Encoding.Default);
try
{
if(sr.ReadLine() != null)
{
this.txtTips.Text = "";
this.txtTips.Text = sr.ReadLine();
}
}
catch(Exception ce)
{
sr = new StreamReader(dir + "\\DOC\\每日一贴.txt");
}
请高手解答。在线等
路径:
sr = new StreamReader(dir + "\\DOC\\每日一贴.txt",System.Text.Encoding.Default);
try
{
if(sr.ReadLine() != null)
{
this.txtTips.Text = "";
this.txtTips.Text = sr.ReadLine();
}
}
catch(Exception ce)
{
sr = new StreamReader(dir + "\\DOC\\每日一贴.txt");
}
请高手解答。在线等
每日一贴.txt,这个文件,是按什么格式存的?
读出来的东西,是放到那个文本框里就行了吗.
<TipsOfTheDay>
<LastTipId>2/LastTipId>
<Tips>
<Tip id="1">
<![CDATA[
Internet Explore 7 增加了反钓鱼机制,它能是你远离已知的恶意钓鱼网站
]]>
</Tip>
<Tip id="2">
<![CDATA[
Internet Explore 7 可以实现标签浏览。您不必打开多个 Internet Explorer 窗口即可浏览多个网页。
]]>
</Tip>
<Tip id="3">
<![CDATA[
更多的 Tip ]]>
</Tip>
<Tip id="4">
<![CDATA[
更多的 Tip ]]>
</Tip>
<!-- 其它的 Tip -->
</Tips>
</TipsOfTheDay>程序开始时,先读取 LastTipId ,然后根据这个 Id 去找下一条 Tip。
XmlDocument doc = new XmlDocument();
doc.Load("Tip.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodes = nav.Select("/TipsOfTheDay/LastTipId");
nodes.MoveNext();
int tipId = nodes.Current.ValueAsInt;
int nextTipId = tipId + 1; // 计算下一条 Tip 的 Id
if(nextTipId > MaxTipId) nextTipId = 1;
string xpath = String.Format("/TipsOfTheDay/Tips/Tip[@id={0}]", nextTipId.ToString());
nodes = nav.Select(xpath);
nodes.MoveNext();
string tip = nodes.Current.Value;上面的代码适当修改,很容易变为获取上一条/下一条的代码。
如果真的要读文本文件,那么,你只能规定好格式。如楼上说的,要保存好每条 Tip 位置,设置 流的 Position 属性来读
www.sina.com.cn/aaa.html
www.17173.com/bbb.html
...那么,
static string dir="c:";
StreamReader sr=new StreamReader((dir+@"\每日一贴.txt"),System.Text.Encoding.Default);
private void button1_Click(object sender, System.EventArgs e)//一个按纽事件.
{
try
{ string values;
if((values=sr.ReadLine()) != null)
{
this.txtTips.Text = "";
this.txtTips.Text =values ;
}
}
catch(Exception ce)
{
sr = new StreamReader(dir + "\\DOC\\每日一贴.txt");
}
}
a就是全部的内容 ,怎么处理都可以
StreamReader sr = new StreamReader(fs,Encoding.Default);
fs.Seek(14, SeekOrigin.Begin);
Console.WriteLine(sr.ReadLine());
sr.Close();
fs.Close();
Console.Read();FileStream 中有Seek方法,它是对文件流指针的重新定位,每日一贴实现的方法有很多,不过通常我都是把它预先读入到内存中,读完之后立即关闭文件。