using System.IO;class test { public static void Main() { StreamWriter sw = File.CreateText("test.txt"); sw.WriteLine("This is a test."); sw.Close(); } }
File.WriteAllText最简单了 public static void Main() { string path = @"c:\temp\MyTest.txt"; // This text is added only once to the file. if (!File.Exists(path)) { // Create a file to write to. string createText = "Hello and Welcome" + Environment.NewLine; File.WriteAllText(path, createText); } // This text is always added, making the file longer over time // if it is not deleted. string appendText = "This is extra text" + Environment.NewLine; File.AppendAllText(path, appendText); // Open the file to read from. string readText = File.ReadAllText(path); Console.WriteLine(readText); }
using System.IO;StreamWriter sw = new StreamWriter("c:\xxx.txt", false); sw.WriteLine("一行"); sw.Close();
StreamWriter sw = new StreamWriter(filepath, false, System.Text.Encoding.GetEncoding("gb2312")); //写内容 int rows_count = _dataGridView1.Rows.Count; int columns_count = _dataGridView1.Columns.Count; for (int j = 0; j < _dataGridView1.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < _dataGridView1.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += _dataGridView1.Rows[j].Cells[k].Value == null ? "" : _dataGridView1.Rows[j].Cells[k].Value.ToString().TrimEnd(); } sw.WriteLine(tempStr.TrimEnd(null)); tempStr = ""; } sw.Close(); sw.Dispose();
using System.IO; StreamWriter sw = new StreamWriter("c:\xxx.txt", false) String s="abc\r\ndef\r\nhijk"; fs.write(s.getBytes()); fs.close();
System.IO.File.AppendAllText("要导出的文件路径", "内容");
using System.IO;[code=C#] 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(); }code]
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(); }
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(); }
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()
二、读文件代码片断: 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 ); } }
{
public static void Main()
{
StreamWriter sw = File.CreateText("test.txt");
sw.WriteLine("This is a test.");
sw.Close();
}
}
String s="abc\r\ndef\r\nhijk";
fs.write(s.getBytes());
fs.close();
{
string path = @"c:\temp\MyTest.txt"; // This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
} // This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText); // Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
sw.WriteLine("一行");
sw.Close();
int rows_count = _dataGridView1.Rows.Count;
int columns_count = _dataGridView1.Columns.Count;
for (int j = 0; j < _dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < _dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += _dataGridView1.Rows[j].Cells[k].Value == null ? "" : _dataGridView1.Rows[j].Cells[k].Value.ToString().TrimEnd();
}
sw.WriteLine(tempStr.TrimEnd(null));
tempStr = "";
}
sw.Close();
sw.Dispose();
StreamWriter sw = new StreamWriter("c:\xxx.txt", false)
String s="abc\r\ndef\r\nhijk";
fs.write(s.getBytes());
fs.close();
System.IO.File.AppendAllText("要导出的文件路径", "内容");
using System.IO;[code=C#] 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();
}code]
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();
}
{
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();
}
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()
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 );
}
}
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
{
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();
}
}