◆◇◆◇◆为什么 StreamWriter.Write 不能写中文?◆◇◆◇◆

wanbo 2005-01-27 04:43:47
部分代码如下:
==============================================================================
.aspx
==============================================================================
<asp:TextBox ID="txtFileContent" TextMode="MultiLine" BackColor="#dff8ff" Font-Size="9" Font-Name="新宋体" Width="98%" Rows="30" runat="server"/>
==============================================================================
.cs
==============================================================================
string strPath = txtPath.Text;
if(!strPath.StartsWith("/"))
strPath = "/" + strPath;
StreamWriter sw = File.CreateText(Server.MapPath(strPath));
sw.Write(txtFileContent.Text);
sw.Close();
==============================================================================
...全文
137 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanbo 2005-01-27
  • 打赏
  • 举报
回复
编译器错误信息: CS1502: 与“System.IO.StreamWriter.StreamWriter(System.IO.Stream, System.Text.Encoding)”最匹配的重载方法具有一些无效参数

源错误:



行 69: if(!strPath.StartsWith("/"))
行 70: strPath = "/" + strPath;
行 71: StreamWriter sw = new StreamWriter(File.CreateText(Server.MapPath(strPath)),System.Text.Encoding.GetEncoding("GB18030"));
行 72: sw.Write(txtFileContent.Text);
行 73: sw.Close();

wanbo 2005-01-27
  • 打赏
  • 举报
回复
编译器错误信息: CS1501: 重载“CreateText”方法未获取“2”参数

源错误:



行 69: if(!strPath.StartsWith("/"))
行 70: strPath = "/" + strPath;
行 71: StreamWriter sw = new StreamWriter(File.CreateText(Server.MapPath(strPath),Encoding.GetEncoding("GB18030")));
行 72: sw.Write(txtFileContent.Text);
行 73: sw.Close();

羊羽亻子 2005-01-27
  • 打赏
  • 举报
回复
完全同意welshem(天堂客) 的说法
fellingsky 2005-01-27
  • 打赏
  • 举报
回复
Encoding.Default
Layto 2005-01-27
  • 打赏
  • 举报
回复
welshem(天堂客), 的正确
wanbo 2005-01-27
  • 打赏
  • 举报
回复
读取中文正常,但写入的时候就会变乱。有的中文正确,有的动变掉了。
LoveCherry 2005-01-27
  • 打赏
  • 举报
回复
Encoding.GetEncoding("GB18030")
这个
wanbo 2005-01-27
  • 打赏
  • 举报
回复
我读取的时候就已经使用了:

StreamReader sr = new StreamReader(File.Open(Server.MapPath(strPath),FileMode.Open),System.Text.Encoding.GetEncoding("GB2312"));
txtFileContent.Text = sr.ReadToEnd();
sr.Close();
welshem 2005-01-27
  • 打赏
  • 举报
回复
StreamWriter sw = new StreamWriter(File.CreateText(Server.MapPath(strPath)) ,Encoding.GetEncoding("GB18030"));
welshem 2005-01-27
  • 打赏
  • 举报
回复
StreamWriter sw = File.CreateText(Server.MapPath(strPath));
改为
StreamWriter sw = new StreamWriter(File.CreateText(Server.MapPath(strPath),Encoding.GetEncoding("GB18030"));
trnbo 2005-01-27
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader( filepath,System.Text.Encoding.GetEncoding( 936 ));

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧