views:

20

answers:

1

My asp.net(c#) method looks as follows:

static public bool GetVideoLength(string fileName, out double length) { DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph(); DirectShowLib.IGraphBuilder graphBuilder; DirectShowLib.IMediaPosition mediaPos; length = 0.0;

        try
        {
            graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
            graphBuilder.RenderFile(fileName, null);
            mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
            mediaPos.get_Duration(out length);

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            mediaPos = null;
            graphBuilder = null;
            graphFilter = null;
        }
    }

I got the duration with the above method. But my problem is i can't delete the physical file after my operation. I used

File.Delete(FilePath);

While performing this action i got an exception as follows:

"The process cannot access the file because it is being used by another process."

My Os is windows 7(IIS 7)

Any one please help me to sort this out?

A: 

I've got no experience in coding directshow apps in C#, but plenty of experience in C++.

DirectShow is based on a technology called COM - which uses reference counting to tell it when an object is in use.

It would use a COM object to represent the IGraphBuilder for example.

In C++, we would have to deconstruct the graph, by removing all its filters, then release the graph.

I understand that C# has its own garbage collection etc., but unless you explicitly release the objects you use, they'll remain in memory.

It seems from the code you've quoted, that the graph is still opened, even though playback may have finished. In that case, it'll hold a reference to the file which you've played back, which would explain why you can't delete it - e.g. there's a read lock on the file.

Hope this points you in the right direction!

freefallr