gpt4 book ai didi

c# - 如何让 ffmpeg 在 try/catch 上为工作目录和参数抛出异常?

转载 作者:行者123 更新时间:2023-12-04 23:32:01 25 4
gpt4 key购买 nike

例如,如果我将 FileName(outputDirectory) 设置为 null,它将引发异常。
我使用 ffmpeg.exe 进程时的类:

public void Start()
{
process = new Process();
process.StartInfo.FileName = outputDirectory; // Change the directory where ffmpeg.exe is.
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = workingDirectory; // The output directory
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true; //Redirect stdin
process.StartInfo.CreateNoWindow = true;
process.Start();

errorMessage = false;
startRecord = true;
}
但如果我将 WorkingDirectory 设置为 null 或输入一些无效的参数,如“sadasdasd”,它不会在 WorkingDirectory 和参数上抛出异常。
在 Form1 中:
private void recordStripMenuItem_Click(object sender, EventArgs e)
{
recordToggle = !recordToggle;

if (recordToggle)
{
try
{
record.workingDirectory = settingsForm.workingDirectory;
record.outputDirectory = settingsForm.outputDirectory;
record.arguments = settingsForm.arguments;
record.Start();
}
catch(Exception ex)
{
recordStripMenuItem.Text = "Record";
Icon = iconGreen;
TextInfo("Waiting");
recordToggle = false;
MessageBox.Show("Arguments are not valid : " + ex.Message);

}
if (!FFmpeg_Capture.errorMessage)
{
settingsForm.Close();
recordStripMenuItem.Text = "Stop";
Icon = iconRed;
TextInfo("Recording");
}
}
else
{
recordStripMenuItem.Text = "Record";
Icon = iconGreen;
TextInfo("Waiting");
record.Stop();
}
}
变量record是FFmpeg_Capture类的实例,带有ffmpeg的进程。
它仅在 FileName(outputDirectory) 上引发异常。

最佳答案

在您的 start() 方法中添加以下代码,如果工作目录不存在则抛出异常:

public void Start(){
if(!System.IO.Directory.Exists(this.workingDirectory){
throw new Exception("workingDirectory doesn't exist");
}

//.. the rest of the code

}

关于c# - 如何让 ffmpeg 在 try/catch 上为工作目录和参数抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72821699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com