[英]C# FileInfo.Length Property for .txt file vs .csv file
I am new to C# and new to Visual Studio.我是 C# 的新手,也是 Visual Studio 的新手。 I am about half way through a 16 week class in C# using Visual Studio.
我使用 Visual Studio 完成了 16 周的 C# 课程。 I felt like I may have learned enough to understand this piece of code from work and modify it.
我觉得我可能已经学到了足够的知识,可以从工作中理解这段代码并对其进行修改。 So far I have been able to understand most of it (after many hours, and using google a lot).
到目前为止,我已经能够理解其中的大部分内容(经过几个小时,并且经常使用谷歌)。 However, there are a few places that have me stumped... Or maybe the original programmer didn't use very good logic?
但是,有几个地方让我难住了... 或者也许原来的程序员没有使用很好的逻辑? I don't know... See the code below:
我不知道...见下面的代码:
//This is just a piece of the code... there are hundreds of lines of code above this
private static void OnSizeChange(object source, FileSystemEventArgs e)
{
try
{
// SET PATHS FROM WATCHER
String filePath = e.FullPath;
FileInfo infoForPath = new FileInfo(e.FullPath);
//CHECK FOR TEXT FILE IN ORDER TO VERIFY SIZE TO CONFIRM NEW EMPTY FILE WAS NOT CREATED
String txtExt = ".txt";
Boolean isTxt = e.FullPath.Contains(txtExt);
//Length gets the size, in bytes, of the current file.
if (!isTxt && infoForPath.Length > 5 || isTxt && infoForPath.Length > 0)
What you can not see here is that the file will either be a .txt file or a .csv file.您在此处看不到的是该文件将是 .txt 文件或 .csv 文件。 My question is about the if statement.
我的问题是关于 if 语句。
What is the if statement checking?什么是 if 语句检查?
From what I can gather, it is checking to see if there is a ".txt" in the file path && the length of the file in bytes is "> 5" (for a non .txt file) or "> 0" (for a .txt file).据我所知,它正在检查文件路径中是否有“.txt”&&文件的长度(以字节为单位)是“> 5”(对于非 .txt 文件)或“> 0”(对于 .txt 文件)。
What is the reason for the "5" and the "0"? “5”和“0”的原因是什么?
Is there some inherent reason for these numbers as pertains to .txt and .csv files?这些数字是否与 .txt 和 .csv 文件有关?
If it helps, i found this code online, which is similar and could be used for testing I think from a C# command prompt application.如果有帮助,我在网上找到了这段代码,它很相似,可以用于我认为从 C# 命令提示符应用程序进行的测试。
using System;
using System.IO;
class Program
{
static void Main()
{
const string fileName = @"C:\programs\file.txt";
// Part 1: create new FileInfo get Length.
FileInfo info = new FileInfo(fileName);
long length = info.Length;
// Part 2: print length in bytes.
Console.WriteLine("LENGTH IN BYTES: {0}", length);
}
}
Output
LENGTH IN BYTES: 60
To start Boolean isTxt = e.FullPath.Contains(txtExt);
开始
Boolean isTxt = e.FullPath.Contains(txtExt);
is error prone and not the best way to do this.容易出错,而不是执行此操作的最佳方法。
You should instead get the extension by doing var fileExtenstion = infoForPath.Extension
this will get you the extension of the file.您应该通过执行
var fileExtenstion = infoForPath.Extension
来获取扩展名,这将为您提供文件的扩展名。 For more info about this look here .有关此外观的更多信息,请访问此处。 Now that you have the extension you can check if the extension is .txt and return a bool or change how you're if statement works.
现在您有了扩展名,您可以检查扩展名是否为 .txt 并返回一个 bool 或更改 if 语句的工作方式。
The reason for checking for the length of 0 for text files is because text files contain no data (length) when they are empty.检查文本文件长度为0的原因是因为文本文件为空时不包含任何数据(长度)。 I don't know for sure but CSV files may have a default length of 5. You can use the console app code you posted if you want to check this
我不确定,但 CSV 文件的默认长度可能为 5。如果您想检查这个,可以使用您发布的控制台应用程序代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.