如何延迟 vb.net 程序直到文件操作完成? [英] How do I delay a vb.net program until a file operation completes?

查看:41
本文介绍了如何延迟 vb.net 程序直到文件操作完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

    Dim myTemp As String
    myTemp = System.DateTime.Now().ToString("MMMddyyyy_HHmmss") & ".pdf"

    System.IO.File.Copy(myFile, "c:\" & myTemp)
    Application.DoEvents()
    OpenFile(myTemp)

问题是当我调用 OpenFile 时,它​​只是调用一个打开文件的子程序,它找不到该文件.这是因为它调用它的速度太快,以至于程序在打开之前没有时间实际创建文件.

The problem is that when I call OpenFile, which is just a call to a sub that opens a file, it cannot find the file. This is because it is calling it so quickly that the program doesn't have time to actually create the file before the open takes place.

我认为 DoEvents() 会纠正这个问题,但事实并非如此.我需要等到文件创建后才能打开文件.我该怎么做?

I thought that DoEvents() would rectify this but it does not. I need to wait until the file is created before I open the file. How can I do that?

推荐答案

我对 VB.NET 了解不多,但 Copy 不是阻塞调用吗?您确定您不是只是试图从错误的位置打开文件(或者未转义的反斜杠使路径无效)?

I don't really know much VB.NET, but isn't Copy a blocking call? Are you sure you're not just trying to open the file from the wrong location (or the unescaped backslash invalidates the path)?

这个呢?我已将驱动器号添加到 OpenFile,并在两个地方都转义了反斜杠.

What about this? I've added the drive letter to OpenFile, and escaped the backslash both places.

Dim myTemp As String
myTemp = System.DateTime.Now().ToString("MMMddyyyy_HHmmss") & ".pdf"

System.IO.File.Copy(myFile, "c:\\" & myTemp)
OpenFile("c:\\" & myTemp)

这篇关于如何延迟 vb.net 程序直到文件操作完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆