C# 无法访问该文件,因为另一个使用ffmpeg的进程正在使用该文件

C# 无法访问该文件,因为另一个使用ffmpeg的进程正在使用该文件,c#,process,ffmpeg,C#,Process,Ffmpeg,我得到一个错误: Cannot access the file because it is being used by another process 我有一个C#桌面应用程序 我正在使用Process类通过使用FFMPEG将图像转换为视频文件 这是我的代码: using (Process serverBuild = new Process()) { serverBuild.StartInfo.WorkingDirectory = Environment.CurrentDirector

我得到一个错误:

Cannot access the file because it is being used by another process
我有一个C#桌面应用程序

我正在使用Process类通过使用FFMPEG将图像转换为视频文件

这是我的代码:

using (Process serverBuild = new Process())
{
    serverBuild.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
    string args = " -f image2  -i " + {path} + "\\img%05d.jpg -s 352x288  -filter:v \"setpts=5.0*PTS\" -y " + {path}\\File.mp4;

    serverBuild.StartInfo.Arguments = args;
    serverBuild.StartInfo.FileName = "ffmpeg.exe";
    serverBuild.StartInfo.UseShellExecute = false;
    serverBuild.StartInfo.RedirectStandardOutput = true;
    serverBuild.StartInfo.RedirectStandardError = true;
    serverBuild.StartInfo.CreateNoWindow = true;
    serverBuild.Start();
    //  string output = serverBuild.StandardOutput.ReadToEnd();
    //Log.Instance.Debug(serverBuild.StandardError.ReadToEnd());
    serverBuild.WaitForExit();
    serverBuild.Close();

}

 Directory.Delete(ExportRoute + FFMPEGPacket.LicenseKey + "\\" + FFMPEGPacket.Guid, true);

//which raise the error..
所有图像都已删除,但文件.Mp4未删除,这就是错误所在。错误表示无法删除新创建的MP4文件

铌 这是说明错误的部分代码

请尝试:

File.WriteAllBytes(path, new byte[0]);
File.Delete(path);

FFMPEG可能在关闭后仍在渲染从图像创建的视频,因此,如果您放置一个线程.Thead.Sleep(5000)5秒;删除之前。

您可以尝试以下代码来创建文件(它对我有效):


抱歉,另一个错误:对路径“d:\Cloud\Exports\EC462-4B771-B1DCC-D5730\33e50f75-0ec8-4bec-ad27-7025d6502dcd”的访问被拒绝。如何在内部使用(File.Create(path))块进行尝试?在:File.writealBytes(ExportRoute+FFMPEGPacket.LicenseKey+“\\”+FFMPEGPacket.Guid,新字节[0]);不确定你的建议是什么?为什么我要创建一个目录?我正在尝试删除该文件?我想接管该文件的使用权等。最后你看了这里吗?嗨,好的,'WaitForExit'应该可以工作,但我已经添加了Thread.Sleep(10000);到我的代码,仍然没有工作。多痛苦啊!嗯,顺便问一下,你的目标是什么?是否要删除整个文件夹?如果没有,为什么不删除除.Mp4文件之外的每个文件?我的目标是删除整个文件夹喔,这很难。如果您的应用程序关闭(最后不删除)。可以手动删除文件夹吗?是的,我试过了。不过我不必这么做,因为“使用”语句对我有帮助,谢谢你的回答。我会尽快查出来的。谢谢
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = exe_path;
        // replace your arguments here
        psi.Arguments = string.Format(@" arguments ") 

        psi.CreateNoWindow = true;
        psi.ErrorDialog = true;
        psi.UseShellExecute = false;
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardInput = false;
        psi.RedirectStandardError = true;
        Process exeProcess = Process.Start(psi);
        exeProcess.PriorityClass = ProcessPriorityClass.High;
        string outString = string.Empty;
        exeProcess.OutputDataReceived += (s, e) =>
        {
            outString += e.Data;
        };
        exeProcess.BeginOutputReadLine();
        string errString = exeProcess.StandardError.ReadToEnd();
        Trace.WriteLine(outString);
        Trace.TraceError(errString);
        exeProcess.WaitForExit();
        exeProcess.Close();
        exeProcess.Dispose();