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. String.IsNullOrEmpty 方法

    参数 value:一个String引用 返回值 如果 value 参数为 空引用(在 Visual Basic 中为 Nothing) 或空字符串 (""),则为 true:否则为 ...

  2. AFNetworking教程

    转:http://www.lanrenios.com/tutorials/network/2012/1126/527.html AFNETWORKING AFNetworking他是一个现在非常用得多 ...

  3. WebView介绍

    本文主要对WebView进行介绍,包括webView 4个可以定制的点.设置WebView back键响应.控制网页的链接仍在webView中跳转.显示页面加载进度.处理https请求.利用addJa ...

  4. 浅淡C/C++中的typedef和#define

    在C/C++中,我们平时写程序可能经常会用到typedef关键字和#define宏 定义命令,在某些情况下使用它们会达到相同的效果,但是它们是有实质性的区别,一个是C/C++的关键字,一个是C/C++ ...

  5. Python--类使用

    类使用的几个注意点: 1. 类的语法结构:2. __init__(self),3. __metaclass__=type, (新式类)4. super(subclassname, self).__in ...

  6. Android FragmentActivity+viewpager的使用

    使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能. package com.lanyuweng.mibaby; import android.content.Intent; impo ...

  7. 利用ASP.NET MVC源代码调试你的应用程序[转]

    由于项目需要,最近学起asp.net mvc.昨天遇到ViewData和TempData他们之间的分别这样让我纠结的问题.有园友强烈建议我去看ASP.NET MVC的源代码.所以,我想到如何在调试AS ...

  8. HTML 5:绘制旋转的太极图

    HTML: <!DOCTYPE> <html> <head> <meta charset="utf-8" /> <title& ...

  9. 集合-Collection

    集合中存放的依然是对象的引用,而不是对象本身 ArrayList: 1) ArrayList底层使用数组实现,当使用不带参数的构造方法生成ArrayList对象时,实际上会在底层生成一个长度为10的O ...

  10. J2EE到底是什么

    目前所有的B/S系统应用可以分为:有状态(statefull)和无状态(stateless)两大类别. 有状态是指在整个系统的处理过程中要保留记住一些信息,而无状态则相反,每次request都是独立的 ...