5

I am writing a PowerPoint add-in that FTPs a file that has been converted to a WMV.

I have the following code which works fine:

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

But this kicks off a process within PP which does the file conversion and it immediately goes to the next line of code before the file has finished being written.

Is there a way to detect when this file has finished being written so I can run the next line of code knowing that the file has been finished?

Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • What is `CreateVideo`? I assume then that it threads the work, and that's why your code moves to the next line immediately? – DonBoitnott Jul 12 '13 at 10:58
  • @DonBoitnott Im not exactly sure to be honest. Either one of the lines of code could be kicking off the conversion process. Is there a way to monitor when those threads have finished? – Trevor Daniel Jul 12 '13 at 11:09
  • 1
    possible duplicate of [Is there a way to check if a file is in use?](http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – DonBoitnott Jul 12 '13 at 11:16
  • @TrevorDaniel "Is there a way to monitor when those threads have finished?" read about Thread.Join() – VladL Jul 12 '13 at 11:22

1 Answers1

13

When a file is being used it is unavailable, so you could check the availability and wait until the file is available for use. An example:

    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;
    }
dsfgsho
  • 2,731
  • 2
  • 22
  • 39
  • 2
    Pretty costly isn't it, using an exception for this? – DonBoitnott Jul 12 '13 at 11:12
  • Well, it seems to be the best way to check whether a file is available. – dsfgsho Jul 12 '13 at 11:14
  • I'm seeing that through SO search as well. Perhaps it is. – DonBoitnott Jul 12 '13 at 11:15
  • 1
    So you're saying the file is available if it throws an exception? Not saying that's wrong, I just thought it would be the other way around – Serberuss Jul 12 '13 at 12:55
  • @StevenHouben thanks steven. I have now added your code which would appear to work :) but for some reason, PowerPoint now starts to create the file. (I can see it on my disk with a size of 0kb) but it no longer starts the process that does the conversion. Im completely stumped. – Trevor Daniel Jul 12 '13 at 13:32
  • PPT has a property that tells you what the current status of a media export is. See my reply to your other post. – Steve Rindsberg Jul 12 '13 at 17:01
  • Can't you use a FileSystemWatcher? – crush Mar 11 '15 at 21:59
  • 1
    Many times, using FileSystemWatcher actually necessitates some way of determining if a file is still writing since the Created event gets thrown before the file is completely written. I usually queue created files for an availability check before processing. – jamesmillerio Feb 13 '17 at 19:39