FileInfo doesn't work in a BackgroundWorkerThread?!? RRS feed

  • Question

  • I have an application that archives our live digital broadcasts.  It is written in C# for the front end and it uses a DLL I wrote using the WMFSDK to archive the files to disk.  I have an interesting problem.

    I use FileInfo in the application itself and it works fine.  I wanted to write a Form/Routine that does an asynch copy of files with progress because all of our files are rather large (about 400-500MB usually).  I have this routine written and it works in XP.  It does not work in Win2k3 or Vista.  I get an error in the exception that isn't one of the normal exceptions for a File in C#.

    The error occurs when I attempt to use a FileInfo inside a class that uses a BackgroundWorkerThread.  This code is in the same project.  FileInfo in the application works, it does not work in the Form/Class that utilizes the BGWT.  Here is the code:

    The file with the problem is definitely the source; here is the code (I have checked, checked and checked again.  The directory structure, the file name, the location and everything is IDENTICAL to the XP box where this code works):

    The problem occurs on the exception block where source is referenced by FileInfo.  At this point I'm merely trying to get the source byte count so the algorithm that calculates the progress on the file copy is computed later on down the road there.

    The error I get is "Could not find file 'c:\dev\source\test.wmv'"

    (The file is there)


            public void CopyFile( string source, string dest )
                long totalFileSize = 0;
                    FileInfo info = new FileInfo ( source );
                    totalFileSize = info.Length;
                catch ( Exception e )
                    MessageBox.Show ( e.Message, "Ha", MessageBoxButtons.OK );

                using ( FileStream sourceStream = new FileStream ( source, FileMode.Open ) )
                    byte [ ] buffer = new byte [ 64 * 1024 ]; // Change to suitable size after testing performance
                    using ( FileStream destStream = new FileStream ( dest, FileMode.Create ) )
                        int i;
                        while ( ( i = sourceStream.Read ( buffer, 0, buffer.Length ) ) > 0 )
                            destStream.Write ( buffer, 0, i );

                            if ( bgWorker.CancellationPending )

                            bgWorker.ReportProgress ( ( int ) Convert.ToInt64 ( ( ( float ) sourceStream.Position / ( float ) totalFileSize ) * 100 ) );

                            System.Threading.Thread.Sleep ( 10 );

    What could possibly be happening here?  I would like to use this code because it doesn't "rape" the machine like File.Copy, which oddly assumes you aren't copying large files.  I need event driven stuff here because I don't want the interface locking up or putting undo pressure on all the other threads that are running (my code can "Tivo" 200 streams at once, that is a lot of thread context switching and disk access that is realtime time dependent).

    Just to summarize, FileInfo works in the app but does not work in this context.

    Any help would be appreciated.  Is this some kind of permissions problem on the BGWT?  I can't imagine but this error is nonsensical.

    Tuesday, November 4, 2008 2:29 PM