【问题标题】:C# random file in use by other process errors其他进程错误正在使用 C# 随机文件
【发布时间】:2011-08-13 17:59:39
【问题描述】:

我编写了一个应用程序,它采用未压缩的 .wav 文件并编码为 flac 文件、mp3 文件和 aac 文件。 flac 和 mp3 工作正常,但在进行 aac 编码(使用 Nero AAC encoder)时,我随机得到关于正在使用的文件的 IOExceptions。有时 - 但不经常 - 它在文件生成时发生 - 我的进程创建的文件如何被另一个文件使用?但更常见的是,当我使用 TagLib 编写元标记时,我得到了错误。因此,我修改了代码以在出现 IO 错误时在相关文件上运行 sysinternal's handle.exe,但它返回的事实是文件上没有句柄。这只是 Nero aac 编码器的固有问题吗?

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 )
    {
        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;
    }

【问题讨论】:

  • 您应该使用FileMonProcMon 之类的工具来查看打开文件的内容。很可能它被索引或病毒扫描非常短暂,因此如果不捕获日志,您将无法看到它。

标签: c# process aac


【解决方案1】:

我建议您使用其他 SysInternals 工具 - filemon.exe

  1. 在抱怨 IOException 的方法上设置断点
  2. 从 VS 以调试模式运行应用程序
  3. 当 VS 在断点处停止时 - 启动 filemon.exe
  4. 在VS中按F5(继续执行)
  5. 出现异常时 - 暂停 filemon.exe 并开始分析文件的情况

为了简化调查,您可以在 filemon 中按文件或进程名称设置过滤器 祝你好运!

【讨论】:

  • 我使用了 procmon.exe,因为 filemon 不再可用。虽然它没有向我显示确切的锁定,但它确实向我显示了 Windows Media Player(wmplayer.exe - 我什至不使用)试图在这些文件被写入时对其进行编目。 this link 导致我在 Windows Sideshow 中禁用了 Windows Media 插件,问题似乎已解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 2011-03-20
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多