通过程序 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. ...
随机推荐
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(7)
检索文档(Retrieving documents) 我们已经有文档存储在我们的实例.现在,让我们尝试检索它们: curl -XGET http://localhost:9200/blog/artic ...
- <译>Selenium Python Bindings 5 - Waits
如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间
打算学习一下Linux,这两天先看了一下gcc的简单用法以及makefile的写法,今天是周末,天气闷热超市,早晨突然发现住处的冰箱可以用了,于是先出去吃了点东西,然后去超市买了一坨冰棍,老冰棍居多, ...
- 关于Cocoapods安装与问题
安装: 1.打开终端 2.如果网络没有FQ的话,需要通过淘宝的RubyGems镜像进行安装. 首先移除默认地址: gem sources --remove https://rubygems.org/ ...
- [LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- matlab特征值分解和奇异值分解
特征值分解 函数 eig 格式 d = eig(A) %求矩阵A的特征值d,以向量形式存放d. d = eig(A,B) %A.B为方阵,求广义特征值d,以向量形式存放d. ...
- Another mysql daemon already running with the same unix socket
在国外网站发现的解决方法. 原因多个Mysql进程使用了同一个socket. 两个方法解决: 第一个是立即关机 使用命令 shutdown -h now 关机,关机后在启动,进程就停止了. 第二个直接 ...
- cos
Apple过于封闭,没啥朋友,这家伙应该比较高傲,曾仅和Intel,IBM and so on..一起玩过!Google过于开放,狐朋狗友,友人泛滥,殃及ecosystem,弊端已显,祸水将至.COS ...
- 第二百三十八天 how can I 坚持
最近睡觉,老是梦到死亡,多么可怕啊.感觉好虚幻. spring事务管理,框架搭建. 看着没多少事,最起来感觉好多啊. 梳理下最近爬过的山,时间久了会忘,反正上周没爬,下雪了. 10月18号-香山,11 ...
- ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题
转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...