怎么读文件? string strHts=MySRead.ReadLine();------> string strHts=MySRead.ReadToEnd(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 while(MySRead.ReadLine()){ //每次读一行数据} 帮我看看这段代码:为什么我用这个方法生成的htm页面有乱码呢?而且不能用数据库中的内容替换。private void Button1_Click(object sender, System.EventArgs e) { MyDataBase MyRead=new MyDataBase(); MyRead.SqlString="select * from tb_news_lm"; string strPath=System.Configuration.ConfigurationSettings.AppSettings["newsPath"].ToString(); for(int i=0;i<MyRead.GetDataSet.Tables[0].Rows.Count;i++) { string strlmID=MyRead.GetDataSet.Tables[0].Rows[i]["L_id"].ToString(); string strDpath=strPath+"/lm_"+strlmID; //创建各个栏目的文件 Directory.CreateDirectory(strDpath); MyDataBase MyReadNews=new MyDataBase(); //在对应的栏目文件夹中生成html文件 string strlmName=MyRead.GetDataSet.Tables[0].Rows[i]["L_name"].ToString(); MyReadNews.SqlString="select * from tb_news where ch_traitsort like'%"+strlmName+"%'"; //Response.Write(MyReadNews.SqlString); for(int j=0;j<MyReadNews.GetDataSet.Tables[0].Rows.Count;j++) { //创建html静态页面 string strArtilceNum=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_articlenum"].ToString();//文章的编号 string StrHtmlName=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_articlenum"].ToString()+".htm"; StreamWriter MySWrite=null; MySWrite=File.CreateText(strDpath+"/"+StrHtmlName); MySWrite.Close(); //读模版 try { StreamReader MySRead=null; MySRead=File.OpenText("c:/Inetpub/wwwroot/zgjt/hts/"+"1-1.hts"); string strHts=MySRead.ReadToEnd(); MySRead.Close(); //用实际内容替换模版 string strTile=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_title"].ToString(); string strAuthor=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_author"].ToString(); string strTime=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_publishdate"].ToString(); string strcontent=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_content"].ToString(); strHts.Replace("[%newstitle%]",strTile).Replace("[%newsauthor%]",strAuthor).Replace("[%newsdate%]",strTime).Replace("[%newstxt%]",strcontent); //生成页面 StreamWriter MyCreatHtml =new StreamWriter(strDpath+"/"+StrHtmlName,true); MyCreatHtml.WriteLine(strHts); MyCreatHtml.Close(); } catch {} } } } 在新闻表中,每条新闻都有它的栏目。生成htm页面的时候,先把新闻根据栏目分类。栏目有自己的栏目表。例如:出租车栏目在栏目表tb_news_lm 中 L_id=100,那么建立名为:lm_100的文件夹,然后把所该栏目的新闻生成静态页面放在其下。为什么有乱码呢?我的模版本内容为:<HTML><HEAD><TITLE> [%newstitle%] </TITLE><META NAME="Generator" CONTENT="EditPlus"><META NAME="Author" CONTENT=""><META NAME="Keywords" CONTENT=""><META NAME="Description" CONTENT=""></HEAD><BODY><center><html><head><title>新闻</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><!-- Fireworks MX Dreamweaver MX target. Created Fri Apr 11 23:56:09 GMT+0800 (?D1ú±ê×?ê±??) 2003--><style type="text/css"><!--.z { font-family: "宋体"; font-size: 9pt; text-decoration: none;}.bd { border: 1px solid #000000;}a { font-family: "宋体"; font-size: 9pt; color: #000000; text-decoration: none;}--></style><script language=javascript>function check_form(){ if(document.frm.topic.value=="") { alert("你的主题是什么?"); document.frm.topic.focus(); return false; } if(document.frm.content.value=="") { alert("说点什么吧! "); document.frm.content.focus(); return false; } if(document.frm.name.value=="") { alert("请写上你的姓名! "); document.frm.name.focus(); return false; }}</script><style type="text/css"> TABLE { FONT-SIZE: 9pt; LINE-HEIGHT: 20px; FONT-FAMILY: "宋体"; TEXT-DECORATION: none } </style></head><body bgcolor="#ffffff" background="images/bj.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><center><TABLE cellSpacing="1" cellPadding="1" width="779" border="0" bgcolor="#ffffff"> <TR> <TD> <IFRAME style=z-index:100 style=WIDTH:779px; HEIGHT:146px marginWidth=0 marginHeight=0 src=http://192.168.1.57/zgjt/top.aspx frameBorder=0 width=770 scrolling=no height=146></IFRAME> </TD> </TR> <TR> <TD> <IFRAME style=z-index:100 style=WIDTH:779px; HEIGHT:28px marginWidth=0 marginHeight=0 src=http://192.168.1.57/zgjt/topguide.aspx frameBorder=0 width=770 scrolling=no height=28></IFRAME> </TD> </TR> </table> <table width="779" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#ffffff"> <tr> <td height="36" class="z"> <div align="center"><font size="3"><b>[%newstitle%]</b></font></div></td> </tr> <tr> <td height="10" class="z"><div align="center">作者:[%newsauthor%] 日期:[%newsdate%] </div></td> </tr> <tr> <td height="61" valign="top" class="z"><div align="left"> <img src="botton/jt.gif" width="14" height="10"> <span style="font-size: 11pt" style="line-height: 150%">[%newstxt%]</span> <br> </div> <p></p> </td> </tr> </table> <table width="779" border=0 cellspacing="0" cellpadding="0" align="center"> <tr> <td><span class="z"><a href=javascript:history.print()>打印</a></span></td> <td><span class="z"><a href="mailto:[email protected]">推荐</a></span></td> <td><span class="z"><a href=javascript:history.back()>返回</a></span></td> <td><span class="z"><a href=javascript:window.close()>关闭</a></span></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align=center> <!--新闻评论--><iframe id="f_news2" name="main" src="http://192.168.1.57/zgjt/news_postView.aspx" frameBorder="0" width="744" scrolling="no" height="1200"></iframe> </td> </tr> </table> <table border=0 width=779 cellspacing=0 cellpadding=0 height=18><tr align=center><td><font face=Verdana,sans-serif,Helvetica,Arial size=1>Copyright © 2003-2004 <b><font color=#cc0000>Goldennews.com.cn</font></b>, All Rights Reserved</font><font class="z"><br>中国交通报社版权所有</font><br><a href=http://www.goldennews.com.cn>北京金报兴图信息工程技术有限公司</a><font class="z">提供技术支持</font><br>E-mail:<a href=mailto:[email protected]><font color=black>[email protected]</font></a></td> </tr> </table></BODY></HTML> 我用上面的方法生成的新闻如下:为什么是乱码呢???瀹胯鲍涓ユ煡闈炴硶閲囩爞鐐?/b> 鐜嬬珛骞筹紝鐜嬫檽姊呞?0030325 鏈姤璁?鏃ュ墠锛屾睙鑻忕渷瀹胯鲍鍘挎按涓婁氦閫氱鐞嗘墍浼氬悓璇ュ幙鍏畨銆佸浗鍦熺瓑澶氬鎵ф硶鍗曚綅浜哄憳杩涢┗楠嗛┈婀栵紝瀵硅姘村煙杩涜鍏ㄩ潰娓呯悊锛屽彇缂旈潪娉曢粍鐮傚紑閲囩偣銆? 銆€ 杩欐缁煎悎鎵ф硶娲诲姩浠ュ厛鍙栫紨鍚庢不鐞嗐€佹不鐞嗗悗鍐嶈鑼冧负鍘熷垯锛屽婀栧唴鎵€鏈夐噰鐮傜偣杩涜鍒嗙被锛岄€愪竴鏌ラ獙锛屽苟鐧昏閫犲唽銆傚悓鏃讹紝瀵归噰鐮傛簮澶寸殑杩愮爞鑸硅埗锛屾墽娉曢儴闂ㄤ緷娉曟牳鍙戠粡钀ヨ鍙瘉鍜岃惀杩愯瘉锛屽緛缂村悇椤逛氦閫氳璐癸紝缁存姢鑹ソ鐨勮繍钀ョЗ搴忥紝鍔姏涓洪獑椹箹钀ラ€犺壇濂界幆澧冦€? StreamReader MySRead=null;MySRead=File.OpenText("C:\\Inetpub\\wwwroot\\zgjt\\HTS\\1-1.htm");string strHts;while(MySRead.ReadLine()){ strHts=strHts+MySread;}TextBox1.Text=strHts;MySRead.Close(); StreamWriter MyCreatHtml =new StreamWriter("c:/a.htm",true);MyCreatHtml.WriteLine("中国解放军");MyCreatHtml.Close();查看后却是:涓浗瑙f斁鍐?为什么.net对文件进行中文的写会出现乱码呢? 可能是编码格式有关试试更改配置文件中的此节:<globalization requestEncoding="utf-8" responseEncoding="gb2312" /> StreamWriter MyCreatHtml =new StreamWriter("c:/a.htm",true);上一句中加上System.Text.Encoding.****** StreamReader MySRead=new StreamReader("C:\\Inetpub\\wwwroot\\zgjt\\HTS\\1-1.htm");TextBox1.Text=MySRead.ReadToEnd();MySRead.Close();這樣也可以吧!!!!不用system.io.File.OpenText() 乱码可能是对汉字的读入不正确;在这里应该采用本方法;string myConnectionString="";FileStream ins=new FileStream (System.IO.Directory.GetCurrentDirectory() +"\\班级.txt" ,FileMode.Open,FileAccess.ReadWrite);System.IO .StreamReader myreader=new StreamReader (ins,System.Text .Encoding .Default );string[] college=new string [200]; char[] c=new char[2];c[0]='\n'; c[1]=',';for(int j=0;j<=this.comboBox4 .SelectedIndex ;j++)myConnectionString=myreader.ReadLine (); college=myConnectionString.Split (c,200); int count=college.Length ; for(int i=1;i<count;i++) Console.WriteLine(college[i]); C# 怎么修改静态类中的 静态私有成员的值啊。急。。。 C#winform中如何点击一个窗体的button,向另一个窗体动态添加button控件 请教windowsmobile,C#怎么操作EXCEL 多线程调用共用方法的问题 高分解决OleDbCommandBuilder更新数据问题 求助输写猜字游戏的客户端游戏 在C#里如何使用USB2。0 web camera 高手请进????99分 送分题 请问C#中的取字符串的子串呢????? 请问在winform下固定DataGrid的列用什么方法? 什么是"静态成员" 什么是"实例成员"?
{
//每次读一行数据
}
private void Button1_Click(object sender, System.EventArgs e)
{
MyDataBase MyRead=new MyDataBase();
MyRead.SqlString="select * from tb_news_lm";
string strPath=System.Configuration.ConfigurationSettings.AppSettings["newsPath"].ToString();
for(int i=0;i<MyRead.GetDataSet.Tables[0].Rows.Count;i++)
{
string strlmID=MyRead.GetDataSet.Tables[0].Rows[i]["L_id"].ToString();
string strDpath=strPath+"/lm_"+strlmID;
//创建各个栏目的文件
Directory.CreateDirectory(strDpath);
MyDataBase MyReadNews=new MyDataBase();
//在对应的栏目文件夹中生成html文件
string strlmName=MyRead.GetDataSet.Tables[0].Rows[i]["L_name"].ToString();
MyReadNews.SqlString="select * from tb_news where ch_traitsort like'%"+strlmName+"%'";
//Response.Write(MyReadNews.SqlString);
for(int j=0;j<MyReadNews.GetDataSet.Tables[0].Rows.Count;j++)
{
//创建html静态页面
string strArtilceNum=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_articlenum"].ToString();//文章的编号
string StrHtmlName=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_articlenum"].ToString()+".htm";
StreamWriter MySWrite=null;
MySWrite=File.CreateText(strDpath+"/"+StrHtmlName);
MySWrite.Close();
//读模版
try
{
StreamReader MySRead=null;
MySRead=File.OpenText("c:/Inetpub/wwwroot/zgjt/hts/"+"1-1.hts");
string strHts=MySRead.ReadToEnd();
MySRead.Close();
//用实际内容替换模版
string strTile=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_title"].ToString();
string strAuthor=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_author"].ToString();
string strTime=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_publishdate"].ToString();
string strcontent=MyReadNews.GetDataSet.Tables[0].Rows[j]["ch_content"].ToString();
strHts.Replace("[%newstitle%]",strTile).Replace("[%newsauthor%]",strAuthor).Replace("[%newsdate%]",strTime).Replace("[%newstxt%]",strcontent); //生成页面
StreamWriter MyCreatHtml =new StreamWriter(strDpath+"/"+StrHtmlName,true);
MyCreatHtml.WriteLine(strHts);
MyCreatHtml.Close(); }
catch
{}
}
}
}
栏目有自己的栏目表。例如:出租车栏目在栏目表tb_news_lm 中 L_id=100,那么建立名为:
lm_100的文件夹,然后把所该栏目的新闻生成静态页面放在其下。
为什么有乱码呢?
我的模版本内容为:
<HTML>
<HEAD>
<TITLE> [%newstitle%] </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<center>
<html>
<head>
<title>新闻</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!-- Fireworks MX Dreamweaver MX target. Created Fri Apr 11 23:56:09 GMT+0800 (?D1ú±ê×?ê±??) 2003-->
<style type="text/css">
<!--
.z {
font-family: "宋体";
font-size: 9pt;
text-decoration: none;
}
.bd {
border: 1px solid #000000;
}
a {
font-family: "宋体";
font-size: 9pt;
color: #000000;
text-decoration: none;
}-->
</style>
<script language=javascript>
function check_form()
{
if(document.frm.topic.value=="")
{
alert("你的主题是什么?");
document.frm.topic.focus();
return false;
}
if(document.frm.content.value=="")
{
alert("说点什么吧! ");
document.frm.content.focus();
return false;
}
if(document.frm.name.value=="")
{
alert("请写上你的姓名! ");
document.frm.name.focus();
return false;
}
}
</script>
<style type="text/css"> TABLE { FONT-SIZE: 9pt; LINE-HEIGHT: 20px; FONT-FAMILY: "宋体"; TEXT-DECORATION: none } </style></head>
<body bgcolor="#ffffff" background="images/bj.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>
<TABLE cellSpacing="1" cellPadding="1" width="779" border="0" bgcolor="#ffffff">
<TR>
<TD>
<IFRAME style=z-index:100 style=WIDTH:779px; HEIGHT:146px marginWidth=0 marginHeight=0 src=http://192.168.1.57/zgjt/top.aspx frameBorder=0 width=770 scrolling=no height=146></IFRAME>
</TD>
</TR>
<TR>
<TD>
<IFRAME style=z-index:100 style=WIDTH:779px; HEIGHT:28px marginWidth=0 marginHeight=0 src=http://192.168.1.57/zgjt/topguide.aspx frameBorder=0 width=770 scrolling=no height=28></IFRAME>
</TD>
</TR>
</table>
<table width="779" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
<tr>
<td height="36" class="z"> <div align="center"><font size="3"><b>[%newstitle%]</b></font></div></td>
</tr> <tr>
<td height="10" class="z"><div align="center">作者:[%newsauthor%] 日期:[%newsdate%] </div></td>
</tr>
<tr>
<td height="61" valign="top" class="z"><div align="left">
<img src="botton/jt.gif" width="14" height="10">
<span style="font-size: 11pt" style="line-height: 150%">[%newstxt%]</span> <br>
</div>
<p></p>
</td>
</tr>
</table> <table width="779" border=0 cellspacing="0" cellpadding="0" align="center">
<tr>
<td><span class="z"><a href=javascript:history.print()>打印</a></span></td>
<td><span class="z"><a href="mailto:[email protected]">推荐</a></span></td>
<td><span class="z"><a href=javascript:history.back()>返回</a></span></td>
<td><span class="z"><a href=javascript:window.close()>关闭</a></span></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align=center>
<!--新闻评论--><iframe id="f_news2" name="main" src="http://192.168.1.57/zgjt/news_postView.aspx" frameBorder="0" width="744" scrolling="no" height="1200"></iframe>
</td>
</tr>
</table>
<table border=0 width=779 cellspacing=0 cellpadding=0 height=18>
<tr align=center>
<td><font face=Verdana,sans-serif,Helvetica,Arial size=1>Copyright
© 2003-2004 <b><font color=#cc0000>Goldennews.com.cn</font></b>, All Rights Reserved</font><font class="z"><br>中国交通报社版权所有</font><br><a href=http://www.goldennews.com.cn>北京金报兴图信息工程技术有限公司</a><font class="z">提供技术支持</font>
<br>E-mail:<a href=mailto:[email protected]><font color=black>[email protected]</font></a></td>
</tr>
</table></BODY>
</HTML>
为什么是乱码呢???瀹胯鲍涓ユ煡闈炴硶閲囩爞鐐?/b>
鐜嬬珛骞筹紝鐜嬫檽姊呞?0030325
鏈姤璁?鏃ュ墠锛屾睙鑻忕渷瀹胯鲍鍘挎按涓婁氦閫氱鐞嗘墍浼氬悓璇ュ幙鍏畨銆佸浗鍦熺瓑澶氬鎵ф硶鍗曚綅浜哄憳杩涢┗楠嗛┈婀栵紝瀵硅姘村煙杩涜鍏ㄩ潰娓呯悊锛屽彇缂旈潪娉曢粍鐮傚紑閲囩偣銆? 銆€ 杩欐缁煎悎鎵ф硶娲诲姩浠ュ厛鍙栫紨鍚庢不鐞嗐€佹不鐞嗗悗鍐嶈鑼冧负鍘熷垯锛屽婀栧唴鎵€鏈夐噰鐮傜偣杩涜鍒嗙被锛岄€愪竴鏌ラ獙锛屽苟鐧昏閫犲唽銆傚悓鏃讹紝瀵归噰鐮傛簮澶寸殑杩愮爞鑸硅埗锛屾墽娉曢儴闂ㄤ緷娉曟牳鍙戠粡钀ヨ鍙瘉鍜岃惀杩愯瘉锛屽緛缂村悇椤逛氦閫氳璐癸紝缁存姢鑹ソ鐨勮繍钀ョЗ搴忥紝鍔姏涓洪獑椹箹钀ラ€犺壇濂界幆澧冦€?
MySRead=File.OpenText("C:\\Inetpub\\wwwroot\\zgjt\\HTS\\1-1.htm");
string strHts;
while(MySRead.ReadLine())
{
strHts=strHts+MySread;
}
TextBox1.Text=strHts;
MySRead.Close();
MyCreatHtml.WriteLine("中国解放军");
MyCreatHtml.Close();查看后却是:
涓浗瑙f斁鍐?为什么.net对文件进行中文的写会出现乱码呢?
<globalization
requestEncoding="utf-8"
responseEncoding="gb2312"
/>
上一句中加上System.Text.Encoding.******
TextBox1.Text=MySRead.ReadToEnd();
MySRead.Close();
這樣也可以吧!!!!
不用system.io.File.OpenText()
string myConnectionString="";
FileStream ins=new FileStream (System.IO.Directory.GetCurrentDirectory() +"\\班级.txt" ,FileMode.Open,FileAccess.ReadWrite);
System.IO .StreamReader myreader=new StreamReader (ins,System.Text .Encoding .Default );
string[] college=new string [200];
char[] c=new char[2];
c[0]='\n';
c[1]=',';
for(int j=0;j<=this.comboBox4 .SelectedIndex ;j++)
myConnectionString=myreader.ReadLine ();
college=myConnectionString.Split (c,200);
int count=college.Length ;
for(int i=1;i<count;i++)
Console.WriteLine(college[i]);