[英]How can I open the last file (.txt) that was created?
I have two programs, One create a .txt
file when you click a button, and named those files with differences names like: "test1.txt
, test2.txt
, test3.txt
...etc." 我有两个程序,一个程序当您单击一个按钮时创建一个.txt
文件,并将这些文件命名为不同的名称,例如: "test1.txt
, test2.txt
, test3.txt
...等”。 (The name of the files are not consecutive). (文件名不是连续的)。 The other program must open the file that was created last, and the only way to know which file was the last, is with "data modified" filter; 另一个程序必须打开最后创建的文件,而知道哪个文件是最后一个的唯一方法是使用“数据已修改”过滤器。 because those file are created inside a specific folder (I use Windows 7 as Operating System). 因为这些文件是在特定文件夹内创建的(我使用Windows 7作为操作系统)。 How can I open the last file that was created by the fist program? 如何打开由拳头程序创建的最后一个文件?
Example: 例:
test1.txt--- data modified: 08/03/2016 06:33am
test3.txt----data modified: 08/03/2016 06:35am
test4.txt----data modified: 08/03/2016 07:26am
test2.txt----data modified: 08/03/2016 09:35am
I want open/read this file test2.txt----data modified: 08/03/2016 09:35am
我想打开/读取此文件
I saw a lot of examples of the tool "FileSystemWatcher", but I really know how to compare the "data modified". 我看到了很多“ FileSystemWatcher”工具的示例,但我确实知道如何比较“修改后的数据”。 Or exists another way to do that? 还是存在另一种方法?
The simplest and most reliable way is to generate file names with time stamp in them. 最简单,最可靠的方法是生成带有时间戳的文件名。 After, when you start reading them, parse the stamp and process all files in desired order. 之后,当您开始阅读它们时,请分析图章并按所需顺序处理所有文件。
Do not rely on system information about files , if that is feasible to your design, as it may miss-lead you on some machines (depends on Windows version, configuration...) 如果对您的设计可行,请不要依赖于文件的系统信息 ,因为这可能会导致您在某些计算机上误导您(取决于Windows版本,配置...)
File.GetLastWriteTime seems to be returning 'out of date' value File.GetLastWriteTime似乎正在返回“过期”值
static void Main(string[] args)
{
var latestModifiedFile = GetLatestModifiedFile(@"Path here");
Console.WriteLine(latestModifiedFile);
System.Diagnostics.Process.Start(latestModifiedFile)
}
static string GetLatestModifiedFile(string directory)
{
var files = Directory.GetFiles(directory);
return files.OrderBy(f => File.GetLastWriteTime(f)).LastOrDefault();
}
And to answer your question about FileSystemWatcher: 并回答关于FileSystemWatcher的问题:
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"Path here";
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
Console.Read();
}
static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("Created file: " + e.FullPath);
System.Diagnostics.Process.Start(e.FullPath);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.