通过程序 VB.Net 或 C# 读取文本文件行数
1, VB.NET 读取 (通过streamReader)
' tmpCount = 0 'Dim tmpSR As New StreamReader(fileFullName, System.Text.Encoding.Default)
'Do While tmpSR.Peek >= 0
' tmpCount = tmpCount + 1
'Loop
'tmpSR.Close()
2,通过VB.NET程序调用cmd命令
调用方法: fileRecordCounts = GetTxtRowCount(file)
fileShortName = System.IO.Path.GetFileName(file) ' 取短路径名
fileCreationDate = System.IO.File.GetCreationTime(file).ToString("yyyy MM dd HH:mm") ‘取文件创建时间
fileSize = New System.IO.FileInfo(file).Length / 1024 ’取文件大小, fileSize 为 KB
Private Function GetFileRowCount_Info(ByVal sFileFullName As String) As String
If (Not File.Exists(sFileFullName)) Then Return "" Dim output As String = ""
Try
Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process() myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True myProcess.Start()
Dim myStreamWriter As StreamWriter = myProcess.StandardInput
myStreamWriter.WriteLine("find /V """" /C " + sFileFullName) myStreamWriter.Close() output = myProcess.StandardOutput.ReadToEnd() myProcess.WaitForExit()
Catch ex As Exception
Console.WriteLine(ex)
Return ""
End Try Return output End Function 'Dim result As Boolean = Int64.TryParse(value, number)
' If result Then
' Console.WriteLine("Converted '{0}' to {1}.", value, number)
' Else
' If value Is Nothing Then value = ""
' Console.WriteLine("Attempted conversion of '{0}' failed.", value)
' End If Public Function GetTxtRowCount(ByVal sFileFullName As String) As Long If (Not File.Exists(sFileFullName)) Then Return -1 Dim sResult As String = GetFileRowCount_Info(sFileFullName)
If (sResult = "") Then Return -1 Dim lResult As Long = 0 Dim lines() As String = sResult.Split(CChar(vbCrLf))
Dim sTmp As String = ""
For Each s As String In lines
sTmp = s.TrimEnd(CChar(vbCrLf)).ToUpper()
If (sTmp = "") Then Continue For If (Not sTmp.Contains(".TXT")) Then Continue For
If (Not sTmp.Contains("----------")) Then Continue For Long.TryParse(sTmp.Split(CChar(":"))(1).Trim(), lResult) ' 这里需要根据实际情况来 Exit For Next Return lResult
End Function 'Public Function GetTxtRowCount(ByVal sFileFullName As String) As Integer ' If (Not File.Exists(sFileFullName)) Then Return -1 ' Dim sResult As String = GetFileRowCount_Info(sFileFullName)
' If (sResult = "") Then Return -1 ' Dim lResult As Integer = 0 ' Dim lines() As String = sResult.Split(System.Convert.ToChar("\n"))
' Dim sTmp As String = ""
' For Each s As String In lines
' sTmp = s.TrimEnd(System.Convert.ToChar("\r")).ToUpper()
' If (sTmp = "") Then Continue For ' If (Not sTmp.Contains(".TXT")) Then Continue For
' If (Not sTmp.StartsWith("----------")) Then Continue For ' Integer.TryParse(sTmp.Split(CChar(":"))(2).Trim(), lResult) ' Exit For ' Next ' Return lResult
'End Function
3, 通过C#程序调用cmd命令
调用方法: long iResult = Common.ConsoleCommand.GetTxtRowCount(sFileName);
public static string GetFileRowCount_Info(string sFileFullName)
{
if (!File.Exists(sFileFullName)) return ""; string output = "";
try
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true; myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput; //myStreamWriter.WriteLine(sFileFullName.Substring(0, sFileFullName.IndexOf(":") + 1));
myStreamWriter.WriteLine("find /V \"\" /C " + @sFileFullName); myStreamWriter.Close(); output = myProcess.StandardOutput.ReadToEnd(); myProcess.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
return output;
} public static long GetTxtRowCount(string sFileFullName)
{
if (!File.Exists(sFileFullName)) return -1; string sResult = GetFileRowCount_Info(sFileFullName);
if (sResult == "")
return -1; long lResult = 0; string[] lines = sResult.Split(System.Convert.ToChar("\n"));
string sTmp = "";
foreach (string s in lines)
{
sTmp = s.TrimEnd(System.Convert.ToChar("\r")).ToUpper();
if (sTmp == "") continue; if (!sTmp.Contains(".TXT")) continue; // 不是.TXT的排除
if (!sTmp.StartsWith("----------")) continue; long.TryParse(sTmp.Split(':')[2].Trim(), out lResult); // 这里需要根据实际情况来
break;
} return lResult;
}
(结束)
通过程序 VB.Net 或 C# 读取文本文件行数的更多相关文章
- Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数
from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...
- python读取文件行数和某行内容
学习记录: python计算文件的行数和读取某一行内容的实现方法 - nkwy2012 - 博客园https://www.cnblogs.com/nkwy2012/p/6023710.html 文本文 ...
- Python读取文件行数不对
对于一个大文件,读取每一个行然后处理,用readline()方法老是读不全,会读到一半就结束,也不报错: 总之处理的行数跟 wc -l 统计的不一样,调试了一下午,改用 with open('xxx. ...
- 小程序实现textarea随输入的文字行数变化高度自动增加
参考链接:https://blog.csdn.net/liuwengai/article/details/78987957 该实现方法是根据上面的链接改编为小程序的实现,代码如下: wxml: < ...
- 读取文本文件时<U+FEFF> 导致的奇怪问题
项目中经常会从一些文本文件中读取数据进行业务处理,最近遇到一个问题,另外一个部门提供一个txt文本给我们进行业务处理,当我们使用字符流读取文本之后,处理时,发现第一行数据无法匹配,其他数据可以正常处理 ...
- C#读取文本文件某一行
某一时候,我们只会读取文本文件内某一行.怎样读?还是用for或foreach循环?其实操作起来,很简单,先看看文本文件,如果你也想用下面的文档来做测试,你可以在这个链接进行拷贝:<VB.NET提 ...
- C++逐行读取文本文件的正确做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 之前写了一个分析huson日志的控制台程序,其中涉及到C++逐行读取文本文件的做法,代码是这样写的: ifstream ...
- MeteoInfoLab脚本示例:读取文本文件绘制散度图
MeteoInfoLab中读取文本文件数据的函数是asciiread,获取文本文件行.列数的函数是numasciirow和numasciicol,和NCL中函数名一致,但都是小写字母.本例中的示例数据 ...
- python读取文本文件
1. 读取文本文件 代码: f = open('test.txt', 'r') print f.read() f.seek(0) print f.read(14) f.seek(0) print f. ...
随机推荐
- request的用法
Request从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,最后是ServerVariables.Request对象按照这样的顺序依次搜索这几个集合中的变量,如果 ...
- [Everyday Mathematics]20150108
设 $f$ 在 $(a,b)$ 上 $n+1$ 次可导, 且 $$\bex \ln\frac{f(b)+f'(b)+\cdots+f^{(n)}(b)}{f(a)+f'(a)+\cdots+f^{(n ...
- 《C++ primer》--第10章
习题10.21 解释map和set容器的差别,以及他们各自适用的情况. 解答: map容器和set容器的差别在于: map容器是键-值对的集合,而set容器只是键的集合: map类型适用于需要了解键与 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- XTUOJ 1248 TC or CF 搜索
这个题一眼看上去不会 然后有人说是网络流 然后我就想怎么建图啊,然后不会(是本蒟蒻太垃圾了),肯定有网络流解法 然后去群里问了gdut的巨巨,他说他队友爆搜+剪枝过了(我也是非常的叹服) 然后我也写了 ...
- Redrain仿酷狗音乐播放器开发完毕,发布测试程序
转载请说明原出处,谢谢~~ 从暑假到现在中秋刚过,我用duilib开发仿酷狗播放器大概经历了50天.做仿酷狗的意图只是看原酷狗的界面比较漂亮,想做个完整一些的工程来练习一下duilib.今天把写好的程 ...
- 利用原生JavaScript获取样式的方式小结
来源:http://www.ido321.com/930.html ps:是获取样式,不是设置样式.若没有给元素设置样式值,则返回浏览器给予的默认值.(论坛整理) 1.element.style:只能 ...
- <Chapter 2>2-2-2.开发Java应用(Developing a Java App)
App Engine的Java网络应用使用了Java Servlet标准接口来和应用服务器交互.一个应用由一个或多个servlet类组成,每个都扩展了(extend)servlet基类.使用一个叫做部 ...
- Hadoop在百度的应用
百度作为全球最大的中文搜索引擎公司,提供基于搜索引擎的各种产品,包括以网络搜索为主的功能性搜索:以贴吧为主的社区搜索:针对区域.行业的垂直搜索.MP3音乐搜索,以及百科等,几乎覆盖了中文网络世界中所有 ...
- RestTemplate中文乱码问题
使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name); // 使用默认的解码 ...