开发者

C# random file in use by other process errors

开发者 https://www.devze.com 2023-03-28 02:57 出处:网络
I have written an app that takes an uncompressed .wav file and encodes into a flac file, mp3 file, and aac file. The flac and mp3 work fine, but when doing the aac encoding (using the Nero AAC encoder

I have written an app that takes an uncompressed .wav file and encodes into a flac file, mp3 file, and aac file. The flac and mp3 work fine, but when doing the aac encoding (using the Nero AAC encoder) I randomly get IOExceptions about the file being in use. Sometimes - but not as often - it happens while the file is generated - How can the file that my process created be in use by another? But more often it is when I write the meta tags using TagLib that I get the errors. So I rigged up the code to run sysinternal's handle.exe on the file in question upon an IO error, but it returns the fact that there are no handles on the file. Is this just an inherent problem with the Nero aac encoder?

try
{
if ( ! Directory.Exists( es.dest ) )
    {
        Directory.CreateDirectory( Path.GetDirectoryName( es.dest ) );
    }
    if ( File.Exists( es.dest ) ) { File.Delete( es.dest ); }
    bool CommandGenerated = enc.GenerateCommandLine();

    string procOutput = String.Empty;
    if ( CommandGenerated )
    {
开发者_如何学JAVA        Console.Out.WriteLine( "Starting:" );
        p = new Process();
        p.StartInfo.WorkingDirectory = Path.GetDirectoryName( current_config_exe );
        p.StartInfo.FileName = current_config_exe;
        p.StartInfo.Arguments = enc.ToString();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        procOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        int exit_code = p.ExitCode;
        p.Close();
        p.Dispose();
        string exit_msg = "Encoder exitcode: " + exit_code + "\n";
        if ( exit_code != 0 )
        {
            Console.WriteLine( "There was a problem. Do you want to continue? (y/n) (Default = y)" );
            string response = Console.ReadLine();
            if ( response.ToLower() == "n" )
            {
                throw new Exception( "Note: " + exit_msg );
            }
        }
        else if ( es.use_taglib_for_meta && enc.metaswitches != null )
        {
            Meta meta = new Meta( es.dest );
            if ( meta.SetMetaTags( enc.metaswitches, true ) )
            {
                WriteLog( "Meta tags successfully wriiten to: \n" + es.dest, true, false );
            }
            else
            {
                WriteLog( "Meta tags NOT wriiten to: " + es.dest, true );
            }
        }
    }
}
catch ( IOException ex )
    {
        Handler.GetHoldingProcess( es.dest );
        Program.WriteLog( "(writing track to file) Error saving file " + es.dest + ":\n" + ex, true );
        return false;
    }


I would suggest you an other SysInternals tool - filemon.exe

  1. Put breakpoint on method which complaining IOException
  2. Run the application in debug mode from VS
  3. When VS stop on breakpoint - launch filemon.exe
  4. Press F5 in VS (continue execution)
  5. When exception raised - pause filemon.exe and start analysing what is going on with file

To simplify investigation you can setup a filter in filemon by file or process name Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消