怎么样将字符导出到.txt文件?

Z_L_H 2009-03-24 02:04:24
我想将字符串用txt文件保存下来,怎么办?
...全文
753 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
itcrazyman 2009-04-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 brookmill 的回复:]
C# codeusing System.IO;

class test
{
public static void Main()
{
StreamWriter sw = File.CreateText("test.txt");
sw.WriteLine("This is a test.");
sw.Close();
}
}
[/Quote]
up
zhoulehua 2009-04-03
  • 打赏
  • 举报
回复
public void ExportToText()
{
if (string.IsNullOrEmpty(textRemindInfoBody.Text))
return;

StringBuilder builder = new StringBuilder();
//builder.AppendLine("提醒内容:");
builder.AppendLine(textRemindInfoBody.Text);
builder.AppendLine("");

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt|*.txt";
sfd.FilterIndex = 0;
sfd.ShowDialog();
if (!string.IsNullOrEmpty(sfd.FileName))
{
try
{
if (dataTran.WriteToTxetFile(sfd.FileName, builder.ToString()))
{
messgeBoxService.ShowTip("导出完成", btnExport);
}
}
catch (Exception ex)
{
messgeBoxService.ShowTip("导出失败", btnExport);
}
}
}

public Class DataTran
{
public BOOL WriteToTxetFile(string path,string strValue)
{
StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine(strValue);
sw.Close();
sw.Dispose();
}
}
yangc_83 2009-04-01
  • 打赏
  • 举报
回复
可以结贴了!
jietuan 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 zzxap 的回复:]
C# code
C#导出txt文件
//导成txt
Response.Clear();
Response.Buffer = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName) + ".txt");
Response.ContentType = "text/plain";
this.EnableViewState = false;
Response.Writ…
[/Quote]


你了解一下System.IO命名空间里面的类,很简单的
jietuan 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 zzxap 的回复:]
C# code
C#导出txt文件
//导成txt
Response.Clear();
Response.Buffer = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName) + ".txt");
Response.ContentType = "text/plain";
this.EnableViewState = false;
Response.Writ…
[/Quote]


你了解一下System.IO命名空间里面的类,很简单的
Myth_NiuNiu 2009-04-01
  • 打赏
  • 举报
回复
哇,还想来挣点分,那么多人给出答案了,相信楼主肯定会了,一起进步!
hiddkiller 2009-04-01
  • 打赏
  • 举报
回复
mark
zzxap 2009-04-01
  • 打赏
  • 举报
回复
[code=C#]
C#导出txt文件
//导成txt
Response.Clear();
Response.Buffer = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName) + ".txt");
Response.ContentType = "text/plain";
this.EnableViewState = false;
Response.Write(sb.ToString());
Response.End();

//导成excel
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
//Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=aa.txt");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType = "application/ms-excel";
Response.ContentType = "text/plain";
this.EnableViewState = false;
// System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
//System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//this.GridView1.RenderControl(oHtmlTextWriter);
Response.Write("sdfasdfasdfasdfasdfasdfasdfasdfadf");
Response.End();
lei1217

[/CODE]
xukaifu 2009-04-01
  • 打赏
  • 举报
回复
没我的戏了

kbtjh 2009-04-01
  • 打赏
  • 举报
回复
答案都出来了!!!
zhoulehua 2009-04-01
  • 打赏
  • 举报
回复
内事不决问Baidu,外事不决问Google
csjtxy 2009-04-01
  • 打赏
  • 举报
回复
baidu一下,很多的
Z_L_H 2009-04-01
  • 打赏
  • 举报
回复
谢谢各位
pbmlly 2009-03-24
  • 打赏
  • 举报
回复
up
panrongzeng 2009-03-24
  • 打赏
  • 举报
回复
C#有没有Tstringlist类的?
zzxap 2009-03-24
  • 打赏
  • 举报
回复
[code=C#]

二、读文件代码片断:
using System;
using System.IO;

public class TestReadFile
{
public static void Main(String[] args)
{
// Read text file C:\temp\test.txt
FileStream fs = new FileStream(@c:\temp\test.txt (这边及一下都是有双引号的), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);

String line=sr.ReadLine();
while (line!=null)
{
Console.WriteLine(line);
line=sr.ReadLine();
}

sr.Close();
fs.Close();
}
}
三、写文件代码:
using System;
using System.IO;

public class TestWriteFile
{
public static void Main(String[] args)
{
// Create a text file C:\temp\test.txt
FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
// Write to the file using StreamWriter class
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine( First Line );
sw.WriteLine( Second Line);
sw.Flush();
}
}
四、拷贝文件:
using System;
using System.IO;

class TestCopyFile
{
public static void Main()
{
File.Copy(c:\\temp\\source.txt, C:\\temp\\dest.txt );
}
}
五、移动文件:
using System;
using System.IO;

class TestMoveFile
{
public static void Main()
{
File.Move(c:\\temp\\abc.txt, C:\\temp\\def.txt );
}
}
[/CODE]
zzxap 2009-03-24
  • 打赏
  • 举报
回复
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db.mdb")
Dim cmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select * from 1", conn)
Dim dst As DataSet = New DataSet
cmd.Fill(dst)
Try


Response.Charset = "UTF-8"
Response.AppendHeader("Content-Disposition", "attachment;filename=text.txt")
Response.ContentEncoding = System.Text.Encoding.Default
Dim tw As System.IO.StringWriter = New System.IO.StringWriter
Dim hw As Web.UI.HtmlTextWriter = New Web.UI.HtmlTextWriter(tw)
Dim i As Integer
Dim j As Integer
For i = 0 To dst.Tables(0).Rows.Count - 1
For j = 0 To dst.Tables(0).Columns.Count - 1

Response.Write(dst.Tables(0).Rows(i)(dst.Tables(0).Columns(j).ColumnName).ToString)
Response.Write(vbTab)
If j = dst.Tables(0).Columns.Count - 1 Then
Response.Write(vbCrLf)
End If
Next
Next
Response.End()

zzxap 2009-03-24
  • 打赏
  • 举报
回复
private void AddlogFile(string Message)
{
StreamWriter W;
DirectoryInfo DirInfo = new DirectoryInfo(Server.MapPath("\\cloud_scorpion"));
Message = DateTime.Now.ToString() + " " + Message + System.Environment.NewLine;
if (DirInfo.Exists == false)
{
Directory.CreateDirectory(Server.MapPath("\\cloud_scorpion"));
}
FileInfo fileinfo = new FileInfo(Server.MapPath("\\cloud_scorpion")+ "\\aaa.txt");
if (fileinfo.Exists == false)
{
W = File.CreateText(Server.MapPath("\\cloud_scorpion")+ "\\aaa.txt");
}
else
{
W = File.AppendText(Server.MapPath("\\cloud_scorpion")+ "\\aaa.txt");
}
W.WriteLine(Message);
W.Close();
}
sxmonsy 2009-03-24
  • 打赏
  • 举报
回复
路过,接分
柳晛 2009-03-24
  • 打赏
  • 举报
回复
using System.IO;

        public static void FTJL(string 字符串) 
{
string path = @"C:\Documents and Settings\Administrator\桌面\文件名.txt";

if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}

StreamWriter sw = new StreamWriter(path, true);

sw.WriteLine(字符串);

sw.Close();
}
加载更多回复(10)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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