从文本文件中读取字符串,其行号为 [英] Reading a string from a text file with its line no

查看:81
本文介绍了从文本文件中读取字符串,其行号为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从文本文件中读取字符串,其行号为C#.net

解决方案

试试这个



  string  [] lines = System.IO.File.ReadAllLines( @  C:\ Users \Public\TestFolder \WriteLines2.txt); 

// 使用foreach循环显示文件内容。
System.Console.WriteLine( WriteLines2.txt =)的内容;
foreach string line in 行)
{
// 使用制表符缩进文件的每一行。
Console.WriteLine( \t + line);
}





详细信息点击此处 [ ^ ]


有一个简单的解决方案...



看看这里: 如何:从文本文件中读取(C#编程指南) [ ^ ]

声明变量外循环:

  int  counter =  0 ; 



逐行显示文本文件:

  string  [] lines = System.IO.File.ReadAllLines( @  D:\\ MyFile.txt的); 

// 使用foreach循环显示文件内容。
System.Console.WriteLine( MyFile.txt =)的内容;
foreach string line in 行)
{
counter + = 1 ;
// 使用选项卡缩进文件​​的每一行。
控制台.WriteLine( {0} {1},counter,line);
}





这就是全部;)



更多:

代码:阅读文本文件(Visual C#) [ ^ ]

如何:阅读来自文件的文本 [ ^


How to read a string from a text file with its line no in C#.net

解决方案

Try this

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
     // Use a tab to indent each line of the file.
     Console.WriteLine("\t" + line);
}



For Details Click Here[^]


There is a simple solution...

Have a look here: How to: Read From a Text File (C# Programming Guide)[^]
Declare variable outside loop:

int counter = 0;


display text file line by line:

string[] lines = System.IO.File.ReadAllLines(@"D:\\MyFile.txt");

 // Display the file contents by using a foreach loop.
 System.Console.WriteLine("Contents of MyFile.txt = ");
 foreach (string line in lines)
 {
      counter+=1;
     // Use a tab to indent each line of the file.
     Console.WriteLine("{0} {1}", counter, line);
 }



That''s all ;)

More:
Code: Reading From a Text File (Visual C#)[^]
How to: Read Text from a File[^]


这篇关于从文本文件中读取字符串,其行号为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆