简体   繁体   English

如何打开最后创建的文件(.txt)?

[英]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.txttest2.txttest3.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.

相关问题 C#-如何读取txt文件中的最后一行 - C# - How i can read last line in txt file 如何从.txt文件创建的列表中删除一行? - How can I delete a line from List created from .txt file? 如何打开在IIS上发布的应用程序的列表框中列出的文件(excel,txt,单词)? - How can I open a file (excel, txt, word) listed in a ListBox in an App published on IIS? 如果其他用户打开应用程序,如何在txt文件中写入字符串并将其用于消息? - How I can write a string in a txt file and used it for a message if a other user open the application? 我无法在Visual Studio 2008中打开mdf文件-像txt文件一样打开文件,而在Managament Studio中则无法打开 - I can't open mdf file in Visual Studio 2008 - file open like txt file and not in managament studio 我如何在MS Access中打开由ASP.NET创建的.mdb格式的文件 - How can i open file in MS access which is created by asp.net in .mdb format 如何删除file.txt的最后一行 - How to Delete the last line of a file.txt 如何在本地主机上打开txt文件并更改内容 - How to open txt file on localhost and change is content 如何在C#中替换txt文件中的单词 - How can I replace a word in a txt file in C# 如何编辑.txt文件中的数字? - How can I edit numbers in my .txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM