c#向txt文件中输入换行!

net_vs 2007-05-14 05:29:03
如何向txt文件中输入换行,使的txt文件可以换行!容易查找里面的文件!
...全文
4832 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
heycoder 2012-06-27
  • 打赏
  • 举报
回复
文本换行符 \r\n
楼上已经有各种示例代码了...
MisterDotNet 2012-06-27
  • 打赏
  • 举报
回复
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();
shaoan886 2012-06-27
  • 打赏
  • 举报
回复
\r\n
writeline
想念旧时光 2012-06-27
  • 打赏
  • 举报
回复
\r\n个人觉得是这个
cena_jin 2012-06-27
  • 打赏
  • 举报
回复
菜鸟来吼吼~!
a793917709 2012-06-26
  • 打赏
  • 举报
回复
我以前 写过 你参考下
起始 直接用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(" }");
}
sihuashanxq 2012-06-26
  • 打赏
  • 举报
回复
写入换行符\r\n
chen3002min 2012-06-25
  • 打赏
  • 举报
回复
Environment.NewLine
plummoon 2012-06-25
  • 打赏
  • 举报
回复
\r\n

Environment.NewLine

学习了!
龙宜坡 2009-09-04
  • 打赏
  • 举报
回复
Environment.NewLine


MS推荐的做法!
龙宜坡 2009-09-04
  • 打赏
  • 举报
回复
Environment.NewLine


MS推荐的做法!
erytbc 2009-09-03
  • 打赏
  • 举报
回复
\r\n
云霄飞车 2009-09-03
  • 打赏
  • 举报
回复
还是顶哈吧。。。
云霄飞车 2009-09-03
  • 打赏
  • 举报
回复
还是顶哈吧。。。
Justin-Liu 2009-09-03
  • 打赏
  • 举报
回复
Console.WriteLine();啊
lonelySurvive 2009-09-03
  • 打赏
  • 举报
回复
上面那个是写入文件,这个是从文件换行读取

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();
}
}
lonelySurvive 2009-09-03
  • 打赏
  • 举报
回复
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();
}
}
}
lonelySurvive 2009-09-03
  • 打赏
  • 举报
回复
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();
}
}
}
qq189640194 2009-09-03
  • 打赏
  • 举报
回复
Environment.NewLine
YL_Show 2009-09-03
  • 打赏
  • 举报
回复
呵呵...
加载更多回复(10)

62,046

社区成员

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

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

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

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