Encoding code = Encoding.GetEncoding("gb2312");
StreamWriter sw = null;
string filepath = HttpContext.Current.Server.MapPath("~");
string str = TextBox1.Text;//根据你自己的改
string filename = System.DateTime.Now.ToString("yyyyMMddmmss") + ".txt";
filepath = filepath + "/" + filename;
try
{
sw = new StreamWriter(filepath, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
StreamWriter sw = null;
string filepath = HttpContext.Current.Server.MapPath("~");
string str = TextBox1.Text;//根据你自己的改
string filename = System.DateTime.Now.ToString("yyyyMMddmmss") + ".txt";
filepath = filepath + "/" + filename;
try
{
sw = new StreamWriter(filepath, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
解决方案 »
- NetMeetingLib 了解的请进
- 请问一套车队的GPS定位系统软件,购买需要多少钱?
- 关于如何提取datagrid中的数据,做某一列的部分值的求和?
- 请高手帮忙:为什么我使用ADO连接数据库时出现这个错误?
- 一个棘手的问题
- Access 裡的查詢![winfrom]
- invalidOperationException异常
- 高手请赐招:HikPlayM4 关于播放.264视频文件问题
- 高分请教datagrid的源码下载
- bs构架下crystal report 怎么实现在线打印??剧给分
- 安装vs2005后,再卸载vs2003时说什么安装程序无法确定安装的有效顺序
- 为什么开发的数据库程序无法更新叫呢?
try
{
sWriter = new StreamWriter(DateTime.Now.ToString("yyyyMMddmmss") + ".txt", false, Encoding.GetEncoding("GB2312"));//构造文本写入流
sWriter.Write(TextBox1.Text);//将TextBox1中的内容写到文本文件中
sWriter.Close();
}
catch (Exception error)
{
MessageBox.Show(error.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sWriter.Close();
}
注:按你要求,就是上面的代码。如果你是为了写日志,最好上面有语句修改如下:
sWriter = new StreamWriter(DateTime.Now.ToString("yyyyMMdd") + ".txt", true, Encoding.GetEncoding("GB2312"));//构造文本写入流开发软件可以最简单,《程序员秘书》提供大量总结性的源代码例子和总结性的文本资料
初学者的好工具,程序员的技巧库详见:http://www.psec.net.cn《程序员秘书》功能展示Flash动画:
http://www.psec.net.cn/psec/程序员秘书.zip