通过程序 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. ...
随机推荐
- K2 blackpearl 安装
转:http://blog.csdn.net/gxiangzi/article/details/8432188 K2是国外的一款BPM引擎,基于MS的Workflow,关于它的详细介绍在我之前一片博客 ...
- git push冲突解决
1. 首先,可以试图用git push origin branch-name推送自己的修改:2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并:如果git pull提示 ...
- 【转】SDP file
SDP file Introduction The Session Description Protocol (SDP) is a format for describing the initiali ...
- Reading or Writing to Another Processes Memory in C# z
http://www.jarloo.com/reading-and-writing-to-memory/ Declarations [Flags] public enum ProcessAccessF ...
- Delphi word编辑
private void but_Table_Click(object sender, EventArgs e) { object Nothing = System.Reflection.Missin ...
- OpenGL超级宝典第5版&&开发环境搭建
参考:http://www.zyh1690.org/build-opengl-super-bible-fifth-edition-development-environment/ 环境搭建的测试环境为 ...
- PHP $_SERVER的详细参数及说明
$_SERVER['PHP_SELF']#当前正在执行脚本的文件名,与documentroot相关. $_SERVER['argv']#传递给该脚本的参数. $_SERVER['argc']#包含传递 ...
- 【C++对象模型】函数返回C++对象的问题
在深入C++对象模型中,对于形如 CObj obj1 = Get(obj2); 的形式,编译器会在将其改变为如下 Get(obj, CObj& obj1); 将赋值操作符左边的变量作为函数的 ...
- 【C++对象模型】构造函数语意学之一 默认构造函数
默认构造函数,如果程序员没有为类定义构造函数,那么编译器会在[需要的时候]为类合成一个构造函数,而[需要的时候]分为程序员需要的时候和编译器需要的时候,程序员需要的时候应该由程序员来做工作,编译器需要 ...
- 【原】Mongodb相关资料
Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...