简体   繁体   English

检查内部循环是否已创建 *txt 文件

[英]Check inside loop if *txt file has been created

My code is searchcing inside a loop if a *txt file has been created.如果已创建 *txt 文件,我的代码将在循环内搜索。 If file will not be created after x time then i will throw an exception.如果在 x 时间后不会创建文件,那么我将抛出异常。 Here is my code:这是我的代码:

 var AnswerFile = @"C:\myFile.txt";

 for (int i = 0; i <= 30; i++)
 {
     if (File.Exists(AnswerFile))
          break;
      await Task.Delay(100);
 }

 if (File.Exists(AnswerFile))
 {
 }
 else
 { 
 }

After the loop i check my file if has been created or not.循环之后,我检查我的文件是否已创建。 Loop will expire in 3 seconds, 100ms * 30times.循环将在 3 秒后过期,100 毫秒 * 30 次。 My code is working, i am just looking for the performance and quality of my code.我的代码正在运行,我只是在寻找我的代码的性能和质量。 Is there any better approach than mine?有比我更好的方法吗? Example should i use FileInfo class instead this?示例我应该使用 FileInfo class 代替这个吗?

  var fi1 = new FileInfo(AnswerFile);
  if(fi1.Exists)
  {
  }

Or should i use filewatcher Class?还是我应该使用文件观察器 Class?

You should perhaps use a FileSystemWatcher for this and decouple the process of creating the file from the process of reacting to its presence.您或许应该为此使用 FileSystemWatcher,并将创建文件的过程与对其存在做出反应的过程分离。 If the file must be generated in a certain time because it has some expiry time then you could make the expiry datetime part of the file name so that if it appears after that time you know it's expired.如果文件必须在某个时间生成,因为它有一些到期时间,那么您可以将到期日期时间作为文件名的一部分,这样如果它在该时间之后出现,您就知道它已过期。 A note of caution with the FileSystemWatcher - it can sometimes miss something ( the fine manual says that events can be missed if large numbers are generated in a short time) FileSystemWatcher 的注意事项 - 它有时会遗漏一些东西( 精美的手册说,如果在短时间内生成大量事件,则可能会遗漏事件)

In the past I've used this for watching for files being uploaded via ftp.过去我用它来查看通过 ftp 上传的文件。 As soon as the notification of file created appears I put the file into a list and check it periodically to see if it is still growing - you can either look at the filesystem watcher lastwritetime event for this or directly check the size of the file now vs some time ago etc - in either approach it's probably easiest to use a dictionary to track the file and the previous size/most recent lastwritedate event.一旦出现创建文件的通知,我就会将文件放入列表中并定期检查它是否仍在增长 - 您可以查看文件系统观察程序 lastwritetime 事件或直接检查文件的大小 now vs前一段时间等 - 在任何一种方法中,使用字典来跟踪文件和以前的大小/最近的 lastwritedate 事件可能是最简单的。 After a minute of no growth I consider the file uploaded completely and I process it.在没有增长一分钟后,我认为文件已完全上传并进行处理。 It might be wise for you to implement a similar delay if using a file system watcher and the files are arriving by some slow generating method如果使用文件系统观察程序并且文件是通过某种缓慢的生成方法到达的,那么实现类似的延迟可能是明智的

Why you don't retrieve a list of files name, then search in the list?为什么不检索文件名列表,然后在列表中搜索? You can use Directory.GetFiles to get the files list inside a directory then search in this list.您可以使用Directory.GetFiles获取目录中的文件列表,然后在此列表中搜索。 This would be more fixable for you since you will create the list once, and reuse it across the application, instead of calling File.Exists for each file.这对您来说更容易解决,因为您将创建一次列表,并在整个应用程序中重复使用它,而不是为每个文件调用File.Exists

Example:例子:

var path = @"C:\folder\"; // set the folder path, which contains all answers files

var ext = "*.txt"; // set the file extension. 
// GET filename list (bare name) and make them all lowercase.
var files = Directory.GetFiles(path, ext).Select(x=> x.Substring(path.Length, (x.Length - path.Length) - ext.Length + 1 ).Trim().ToLower()).ToList();


// Search for this filename 
var search = "myFile";

// Check 
if(files.Contains(search.ToLower()))
{
    Console.WriteLine($"File : {search} is already existed.");
}
else
{
    Console.WriteLine($"File : {search} is not found.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM