c#检测文件是否写入完成

标签 c# .net powerpoint

我正在编写一个 PowerPoint 加载项,用于通过 FTP 传输已转换为 WMV 的文件。

我有以下代码可以正常工作:

oPres.CreateVideo(exportName);
oPres.SaveAs(String.Format(exportPath, exportName),PowerPoint.PpSaveAsFileType.ppSaveAsWMV,MsoTriState.msoCTrue);

但这会在 PP 中启动一个进程,该进程进行文件转换,并在文件完成写入之前立即转到下一行代码。

有没有办法检测此文件何时完成写入,以便我可以在知道文件已完成的情况下运行下一行代码?

最佳答案

当一个文件正在使用时它是不可用的,因此您可以检查可用性并等待文件可用。一个例子:

    void AwaitFile()
    {
        //Your File
        var file  = new FileInfo("yourFile");

        //While File is not accesable because of writing process
        while (IsFileLocked(file)) { }

        //File is available here

    }

    /// <summary>
    /// Code by ChrisW -> http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
    /// </summary>
    protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }

关于c#检测文件是否写入完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612800/

相关文章:

c# - 如何在不使用 AutoMapper 为覆盖属性的每个派生类显式映射的情况下映射虚拟属性?

C# - 传递 DataTable 与 SqlDataReader

c# - 在特定节点后插入 XElements

c# - 有没有办法表示很长的数字?

svg - 使用 Microsoft Office 2010 PIA 将 PPT 转换为 SVG

vba - 连接 PowerPoint 和 Excel

c# - 这个 while 循环中发生了什么?

c# - MemoryCache.Set 返回删除的缓存项

c# - 将数组添加到数组-一对一

vba - PowerPoint VBA : How to set Animation Start event to "With Previous"