c#向txt文件中输入换行!
如何向txt文件中输入换行,使的txt文件可以换行!容易查找里面的文件! --------------------编程问答-------------------- \n--------------------编程问答-------------------- \n\r --------------------编程问答-------------------- newline --------------------编程问答-------------------- newline --------------------编程问答-------------------- \r\n --------------------编程问答-------------------- Stream.writeline() --------------------编程问答-------------------- \r\n --------------------编程问答-------------------- \r\n --------------------编程问答-------------------- Environment.NewLine --------------------编程问答--------------------
以前用\r\n现在用这个 --------------------编程问答-------------------- 加入换行符啊,比如:
早上好\r\n
中午好\r\n
晚上好
上面都那么多人答出来了``````````````
可惜啊,来晚了`````````````````` --------------------编程问答--------------------
多晚都不晚,楼主看样子是不准备结帖的 --------------------编程问答-------------------- 呵呵... --------------------编程问答--------------------
using System;--------------------编程问答-------------------- using System;
using System.IO;
public class TextToFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sw.Close();
}
}
}
using System.IO;
public class TextToFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sw.Close();
}
}
}
--------------------编程问答-------------------- 上面那个是写入文件,这个是从文件换行读取
using System;
using System.IO;
public class TextFromFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}
--------------------编程问答-------------------- Console.WriteLine();啊 --------------------编程问答-------------------- 还是顶哈吧。。。 --------------------编程问答-------------------- 还是顶哈吧。。。 --------------------编程问答-------------------- Environment.NewLine
MS推荐的做法! --------------------编程问答-------------------- Environment.NewLine
MS推荐的做法! --------------------编程问答-------------------- \r\n
Environment.NewLine
学习了! --------------------编程问答-------------------- Environment.NewLine --------------------编程问答-------------------- 我以前 写过 你参考下
起始 直接用StreamWriter。writeline(“”);就OK了
//读取数据库的表名
DataSet dsTable = Sql.GetTableName(conn);
for (int i = 0; i < dsTable.Tables[0].Rows.Count; i++)
{
string tablename = dsTable.Tables[0].Rows[i][0].ToString();
string filename = tablename + ".cs";
string sNameSpace = "Model";
//生成文件夹
DirectoryInfo d = Directory.CreateDirectory(path+ "\\"+sNameSpace);
//生成文件
StreamWriter str = new StreamWriter(d.FullName + "\\" + filename);
//往文件写入
Sql.WriteCS(tablename, str,sNameSpace);
//读取对应表中的列名
DataSet dsColumns = Sql.GetColumnsName(conn, tablename);
for (int j = 0; j < dsColumns.Tables[0].Rows.Count; j++)
{
//读取对应列名的类型
string strType = Sql.GetColumnsType(conn, tablename, dsColumns, j);
//写入类的属性
WriteClassStyle(str, dsColumns, j, strType);
}
Sql.WtiteTableName(tablename, str);
for (int k = 0; k < dsColumns.Tables[0].Rows.Count; k++)
{
//根据列生产封装字段
WriteColumns(conn, tablename, str, dsColumns, k);
}
str.WriteLine(" }");
str.WriteLine("}");
str.Close();
}
}
private static void WriteClassStyle(StreamWriter str, DataSet dsColumns, int j, string strType)
{
str.WriteLine(" private {0} {1};", strType, dsColumns.Tables[0].Rows[j][0].ToString().Substring(0, 1).ToLower() + dsColumns.Tables[0].Rows[j][0].ToString().Substring(1));
str.WriteLine("");
}
private static void WriteColumns(SqlConnection conn, string tablename, StreamWriter str, DataSet dsColumns, int k)
{
string strType;
strType = Sql.GetColumnsType(conn, tablename, dsColumns, k);
string strColumns = dsColumns.Tables[0].Rows[k][0].ToString();
str.WriteLine("");
str.WriteLine(" /// <summary>");
str.WriteLine(" /// 与数据库表列相对应的属性");
str.WriteLine(" /// <summary>");
str.WriteLine(" /// <remark></remark>");
str.WriteLine(" public {0} {1}", strType, strColumns.Substring(0, 1).ToUpper() + strColumns.Substring(1));
str.WriteLine(" {");
str.WriteLine(" get");
str.WriteLine(" {");
str.WriteLine(" return this.{0};", strColumns.Substring(0, 1).ToLower() + strColumns.Substring(1));
str.WriteLine(" }");
str.WriteLine(" set");
str.WriteLine(" {");
str.WriteLine(" this.{0} = value;", strColumns.Substring(0, 1).ToLower() + strColumns.Substring(1));
str.WriteLine(" }");
str.WriteLine(" }");
} --------------------编程问答-------------------- 写入换行符\r\n --------------------编程问答-------------------- 菜鸟来吼吼~! --------------------编程问答-------------------- \r\n个人觉得是这个 --------------------编程问答-------------------- \r\n
writeline --------------------编程问答-------------------- string updateDesc="行1{0}行2{0}行3";
FileStream fs = new FileStream(logfile, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(updateDesc, Environment.NewLine);
sw.Close();
fs.Close(); --------------------编程问答-------------------- 文本换行符 \r\n
楼上已经有各种示例代码了...
补充:.NET技术 , ASP.NET