Python允许以下操作:打开cmd后,我可以启动python,然后运行直接在cmd中返回结果的python命令。 例如,我输入print word而cmd返回word 。 C#是否有类似的东西,或者执行C#代码的唯一方法是将其写入文件然后进行编译? ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在处理语音记录。 我需要使用.exe
文件将Wav
转换为.mp3
文件。 一切都很好,我可以执行此exe,但是当进程以我的输出.mp3
文件结束时,我需要做一些事情。 我知道我的输出目录,但在尚未创建MP3文件之前无法处理。 我知道也许我需要使用Thread.sleep();
或类似的东西,因为我无法在文件不存在之前捕获它。
这是我的代码:
string mp3GuidName = Guid.NewGuid().ToString();
var mp3FilePath = WavFilePath.Replace("finalWavFile", mp3GuidName).Replace("wav", "mp3");
var extrasFilePath = HttpContext.Current.Server.MapPath("/").Replace("DevApp.Web", "Extras");
string strArguments = "/c start " + extrasFilePath + "lame.exe --abr 80 -V5 " + WavFilePath + " " + mp3FilePath;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = strArguments ;
process.StartInfo = startInfo;
process.Start();
var attactment = new Attachment
{
CreatedOn = DateTime.Now,
UpdatedOn = DateTime.Now,
Title = mp3GuidName +".mp3",
Size = _storageProvider.GetFile(mp3FilePath).GetSize(), // here I am trying to get mp3 file but i cant catch it. Because if this wav files size is huge, then convert process is taking time and my mp3 file is not created yet.
FileExtension = _storageProvider.GetFile(mp3FilePath).GetFileType()
};
attactment.MimeType = _storageProvider.GetMimeType(attactment.FileExtension);
attactment.FileUrl = mp3GuidName+".mp3";// file.GetName();
attactment.AttachmentFolderId = folder.Id;
_attachmentRepository.Add(attactment);
我试图使用process.WaitForExit();
但我不能解决这个问题。 我仍然无法访问mp3文件。
那么如何在过程完成时捕获?
最好的祝福。
从参数字符串中删除start
命令参数,您应该可以使用process.WaitForExit();
等待Lame完成编码:
string strArguments = "/c " + extrasFilePath + "lame.exe --abr 80 -V5 " + WavFilePath + " " + mp3FilePath;
但是,您可以通过直接启动lame.exe来简化代码并完全避免与cmd.exe发生这种冲突:
string strArguments = "--abr 80 -V5 " + WavFilePath + " " + mp3FilePath;
...
startInfo.FileName = extrasFilePath + "lame.exe";
startInfo.Arguments = strArguments;
...
下面的一些信息说明为什么在您的方案中使用start
命令参数会适得其反。
使用cmd.exe(或从控制台或批处理文件)执行控制台应用程序(例如lame.exe)通常会阻塞cmd.exe(或控制台/批处理文件),直到控制台应用程序退出。
但是,使用start
命令会将通常阻止控制台程序执行的操作转换为非阻止执行。 使用此参数,在控制台应用程序仍在运行时,cmd.exe(或控制台/批处理文件)将继续执行。 在您的特定情况下,这意味着cmd.exe将在启动lame.exe之后立即退出(因为它没有其他要执行的操作),从而有效地破坏了您等待lame.exe完成的尝试。
编辑:
根据下面的评论,我必须明确指出,我建议的原始解决方案不检查文件是否空闲,而是仅检查文件是否存在 ! 因此,我建议以下内容:
private bool IsBusy(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch ()
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}
用法将是:
while(IsBusy(fileinfo))
{
//just wait
}
原文:您可以使用while循环来查找文件准备就绪的时间:
while(!File.Exists(mp3FileName))
{
//just wait
}
// add the attachment here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.