如何要等到File.Exists?

编程入门 行业动态 更新时间:2024-10-28 16:19:40
本文介绍了如何要等到File.Exists?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序,监听在选定文件夹中的* .log文件。我用 FileSystemWatcher的。

但有一个问题。负责制定该文件的其他应用程序需要以下步骤:

  • 请一个* .gz的文件
  • 解压到txt文件(有些随机文件名)
  • 的* .TXT名称更改为适当的一个与* .log扩展名。
  • 和我不能改变这种行为。

    所以我做了2 FileSystemWatcher的 S代表*。广州和* .txt文件。为什么?因为这个程序有时不能解压GZ文件,有时不会重命名的txt文件到最终* .log文件。

    FileSystemWatcher2 捕捉txt文件,然后(在它被重命名为下次登录1000毫秒大多数情况下)我需要等待一些时间来检查,如果txt文件存在(如果没有,它似乎被重命名为最终的* .log文件)。

    现在的问题是,如何检查文件是否存在,而不 Thread.sleep代码()防止冻结用户界面?

    我希望它是明确的,如果没有,我会尽力来形容它更好。我觉得这是一个复杂的问题。

    一些示例代码:

    看守的GZ文件:

    私人无效fileSystemWatcher_Created(对象发件人,FileSystemEventArgs E) { //这是GZ文件的情况下,如果GZ文件不被其他应用程序自动解压缩//我需要等待,并检查是否GZ解包,如果没有,我自己解压, //然后TXT观察家将赶上 Thread.sleep代码(5000); 如果(File.Exists(e.FullPath)) {试 {字节[] =的DataBuffer新的字节[4096];使用(的System.IO.Stream FS =新的FileStream(e.FullPath, FileMode.Open, FileAccess.Read)) {使用(GZipInputStream gzipStream =新GZipInputStream(FS)) {串fnOut = Path.Combine(path_to_watcher, Path.GetFileNameWithoutExtension(e.FullPath));使用 (的FileStream FSOUT = File.Create(fnOut)) { StreamUtils.Copy(gzipStream,FSOUT,设置DataBuffer); } } } } 赶上{//忽略} } }

    守望者为txt文件:

    私人无效fileSystemWatcher2_Created(对象发件人,FileSystemEventArgs E) { //这是为txt文件 Thread.sleep代码(3500); 如果(File.Exists(e.FullPath)) { //让自己的行动} ,否则 { //让自己的行动} }

    解决方案

    其实FileSystemWatcher对象的被称为由.NET本身单独的线程创建活动的..所以基本上您需要做的绝对没有。你的代码是OK,因为它是

    下面就是证明:

    类节目 {静态无效的主要(字串[] args) { FileSystemWatcher的FW =新FileSystemWatcher的(@C:\temp); fw.Created + = fileSystemWatcher_Created; Console.WriteLine(Thread.CurrentThread.ManagedThreadId); fw.EnableRaisingEvents = TRUE; 到Console.ReadLine(); } 静态无效fileSystemWatcher_Created(对象发件人,FileSystemEventArgs E) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } }

    I have an app, listening for the *.log file in a chosen folder. I used FileSystemWatcher.

    But there is a problem. The other app responsible for making that file takes following steps:

  • Make a *.gz file
  • Unpack it to txt file (some random file name)
  • Change the *.txt name to proper one with *.log extension.
  • And I can not change this behaviour.

    So I made 2 FileSystemWatchers for *.gz, and *.txt files. Why? Because this app sometimes doesn't unpack the gz file, and sometimes doesn't rename the txt file to the final *.log file.

    FileSystemWatcher2 catches txt file, then (in the most cases it is renamed to log in the next 1000ms) I need to wait some time to check if txt file exists (if not, it seems to be renamed to the final *.log file).

    The question is, how to check if file exists without Thread.Sleep() to prevent UI freeze?

    I hope it is clear, if not I will try to describe it better. I think this is a complex problem.

    Some code sample:

    Watcher for gz file:

    private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) { //this is for gz files in case if gz file is not unpacked automatically by other app //I need to wait and check if gz was unpacked, if not, unpack it by myself, //then txt watcher will catch that Thread.Sleep(5000); if (File.Exists(e.FullPath)) { try { byte[] dataBuffer = new byte[4096]; using (System.IO.Stream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read)) { using (GZipInputStream gzipStream = new GZipInputStream(fs)) { string fnOut = Path.Combine(path_to_watcher, Path.GetFileNameWithoutExtension(e.FullPath)); using (FileStream fsOut = File.Create(fnOut)) { StreamUtils.Copy(gzipStream, fsOut, dataBuffer); } } } } catch { //Ignore } } }

    Watcher for txt file:

    private void fileSystemWatcher2_Created(object sender, FileSystemEventArgs e) { //this is for txt file Thread.Sleep(3500); if (File.Exists(e.FullPath)) { //make my actions } else { //make my actions } }

    解决方案

    Actually FileSystemWatcher Created event called in separate thread by .NET itself.. So basically you need to do absolutely nothing. Your code is OK as it is.

    Here is the proof:

    class Program { static void Main(string[] args) { FileSystemWatcher fw = new FileSystemWatcher(@"C:\temp"); fw.Created += fileSystemWatcher_Created; Console.WriteLine(Thread.CurrentThread.ManagedThreadId); fw.EnableRaisingEvents = true; Console.ReadLine(); } static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } }

    更多推荐

    如何要等到File.Exists?

    本文发布于:2023-06-08 14:31:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/583118.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:要等到   File   Exists

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!