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# 读取文本文件行数的更多相关文章

  1. Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数

    from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...

  2. python读取文件行数和某行内容

    学习记录: python计算文件的行数和读取某一行内容的实现方法 - nkwy2012 - 博客园https://www.cnblogs.com/nkwy2012/p/6023710.html 文本文 ...

  3. Python读取文件行数不对

    对于一个大文件,读取每一个行然后处理,用readline()方法老是读不全,会读到一半就结束,也不报错: 总之处理的行数跟 wc -l 统计的不一样,调试了一下午,改用 with open('xxx. ...

  4. 小程序实现textarea随输入的文字行数变化高度自动增加

    参考链接:https://blog.csdn.net/liuwengai/article/details/78987957 该实现方法是根据上面的链接改编为小程序的实现,代码如下: wxml: < ...

  5. 读取文本文件时<U+FEFF> 导致的奇怪问题

    项目中经常会从一些文本文件中读取数据进行业务处理,最近遇到一个问题,另外一个部门提供一个txt文本给我们进行业务处理,当我们使用字符流读取文本之后,处理时,发现第一行数据无法匹配,其他数据可以正常处理 ...

  6. C#读取文本文件某一行

    某一时候,我们只会读取文本文件内某一行.怎样读?还是用for或foreach循环?其实操作起来,很简单,先看看文本文件,如果你也想用下面的文档来做测试,你可以在这个链接进行拷贝:<VB.NET提 ...

  7. C++逐行读取文本文件的正确做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 之前写了一个分析huson日志的控制台程序,其中涉及到C++逐行读取文本文件的做法,代码是这样写的: ifstream ...

  8. MeteoInfoLab脚本示例:读取文本文件绘制散度图

    MeteoInfoLab中读取文本文件数据的函数是asciiread,获取文本文件行.列数的函数是numasciirow和numasciicol,和NCL中函数名一致,但都是小写字母.本例中的示例数据 ...

  9. python读取文本文件

    1. 读取文本文件 代码: f = open('test.txt', 'r') print f.read() f.seek(0) print f.read(14) f.seek(0) print f. ...

随机推荐

  1. wap版和pc版的旋转js

    <script type="text/javascript"> var evt = "onorientationchange" in window ...

  2. GitHub上README.md教程

    最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种编辑博客的语言 ...

  3. 在英文 sql2005中 比较nvarchar 与 varchar的速度

    declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...

  4. [转] C# 泛型类型参数的约束

    啊.紫原文C# 泛型类型参数的约束 在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制.如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误.这些限制 ...

  5. BLOCK 死循环

    __weak typeof(self) weakSelf = self; myObj.myBlock =  ^{     __strong typeof(self) strongSelf = weak ...

  6. redis集群的搭建

    1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...

  7. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  8. RabbitMQ (二)工作队列 -摘自网络

    这篇中我们将会创建一个工作队列用来在工作者(consumer)间分发耗时任务.工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给 ...

  9. [现代程序设计]homework-03

    Homework-03 队员: 11061193 薛亚杰 11061192 周敏轩    11061190 李孟 材料阅读 我们三个人将以下材料仔细阅读,觉得十分受益.下面是我们的总结和分享: 1)代 ...

  10. Tomcat中配置JNDI数据源

    准备工作: Tomcat版本:tomcat6.0以上 下例中均使用MySQL数据库 将对应数据源的jar包和MySQL的驱动包拷贝至tomcat的lib文件夹下 一.全局数据源 1步骤一:配置 在to ...