C#/.NET的可靠FFmpeg封装程序

95

我已经搜索了一段时间,寻找一个可靠的 C#/.NETFFmpeg包装器。但我还没有找到有用的东西。我找到了以下三个项目,但它们似乎都停留在早期Alpha阶段。

FFmpeg.NET
ffmpeg-sharp
FFLIB.NET

因此我的问题是,是否有人知道更成熟的包装器项目?
我不需要一个完整的转码引擎,也不需要工作队列等功能。 只需要一个简单的包装器,这样我就不必调用命令行并解析控制台输出,而是可以进行方法调用并使用进度事件监听器。

请随意提及任何活跃的项目,即使它们仍处于早期阶段。


1
可能是重复的问题:有人知道FFMPEG的C#绑定集吗? - Paŭlo Ebermann
1
这方面有什么新进展吗?你的封装器有没有取得任何进展? - Avi
3
@Lillemanden,你曾经发布或开源过你的封装器(wrapper)吗? - Nick Benedict
有趣的是,这个问题已经快六年了,但楼主 (@JacobPoulRichardt) 没有接受任何答案。 - Ofer Zelig
1
我最终使用了自己制作的包装器,因此没有使用任何建议的项目。由于我不再使用ffmpeg,所以我也没有时间回去尝试它们。但是在浏览后,我已经为大多数答案投了赞成票。因此,我认为没有任何一个答案比其他答案更“正确”。 - Jacob Poul Richardt
12个回答

26

这是我的一个封装: https://github.com/AydinAdn/MediaToolkit

MediaToolkit能够:

  • 将视频文件转换为其他各种视频格式。
  • 执行视频转码任务。
    • 可以配置的选项包括:比特率帧率分辨率/大小宽高比视频持续时间
  • 执行音频转码任务。
    • 可以配置的选项包括:音频采样率
  • 将视频转换为使用FILM、PAL或NTSC电视标准的物理格式
    • 介质包括:DVDDVDV50VCDSVCD

我会随着更新而更新,欢迎您使用它,您也可以使用Package Manager Console安装它。

PM> Install-Package MediaToolkit

嘿,艾丁,这个能记录屏幕吗? - TEK
@TEK 简而言之,不行。简短概括一下,我实际上在考虑制作一个应用程序,你可以通过点击打印屏幕按钮来记录屏幕,然后再次点击完成录制并自动上传,并返回一个可以共享的 URL... 无论如何,如果我没记错的话,FFmpeg 支持录制屏幕... 只是没有在包装器中分离出来。 - Aydin
@AydinAdn 谢谢你,Aydin。看起来我得自己动手了!不过我肯定会以此为基础的。它看起来很棒。 - TEK
尝试一下...FWIW,这仅适用于文件系统上路径中的blob(因为ffmpeg.exe也是如此),但在针对运行时4.5.2的项目中使用它似乎会导致编译器警告,指出“ExtensionAttribute”在2个gac程序集中定义,其中一个是该项目,另一个是mscorlib。 - danludwig
我从git下载了MediaToolkit源代码,它运行得非常好。然后我尝试将rtsp流转换为mp4视频,对于单个视频也可以正常工作...我的意思是视频被构建并保存到目录路径中。 现在我的要求是在循环中同时创建多个视频,并在目录路径中创建多个视频,但每个视频都包含至少1秒的间隔:( 我该如何去除这个间隔,请帮帮我? - Nimesh khatri
显示剩余4条评论

15

尝试了几个封装库后,我选择了这个:FFmpeg 自动生成的 C#/.NET 和 Mono 不安全绑定

它是 FFmpeg 命名空间中每个类的低级别互操作绑定。可能不如实际封装使用方便,但在我看来,如果您想要做非平凡的事情并且想在 .Net 中使用 FFmpeg,则它是最佳解决方案。

优点:

  • 可以使用
  • 可信 - 没有第三方封装代码引入 bug ,假设您信任 FFMpeg 本身。
  • 它始终更新到最新版本的 FFmpeg
  • 单个 nuget 包 包含所有绑定
  • 包括 XML 文档,但您仍然可以使用在线文档 FFmpeg 文档

缺点:

  • 低级别:您必须知道如何使用指向结构体的指针。
  • 需要一些初始工作才能使其正常工作。建议从官方示例 中学习。

注意:此线程是关于使用 FFmpeg API,但对于某些用例,最好仅使用 ffmpeg.exe 的 命令行界面


你能在针对 .Net Framework(而非Core)的项目中使用它吗?我不确定我错过了什么。 - Yoav Feuerstein
@YoavFeuerstein 是的。 - orca

11

我在一个ASP.NET / Windows服务 (.NET)应用程序中使用了FFmpeg。但最终我选择使用命令行而不是解析控制台。通过这种方式,我可以轻松地控制FFmpeg的更新,并在多个核心上运行多个转换。


好的,我已经开始处理类似的问题了。但是我仍然希望有人能提供更好的解决方案。 - Jacob Poul Richardt

10

谢谢,但我已经开始写自己的了。但如果我无法得到一些想法,我会再看看的。 - Jacob Poul Richardt

6
您可以使用此NuGet软件包:
我知道您询问的是成熟的项目,但我没有看到任何一个满足我的期望,所以我决定自己制作。您可以轻松地排队转换并并行运行它,将媒体转换为不同格式的方法,发送自己的参数到FFmpeg,并解析FFmpeg的输出+带有当前进度的事件侦听器。
安装命令:Install-Package Xabe.FFmpeg 我正在尝试制作易于使用的跨平台FFmpeg包装器。
您可以在https://xabe.net/product/xabe_ffmpeg/找到更多信息。
更多信息请参见:https://xabe.net/product/xabe_ffmpeg/#documentation 转换很简单:
IConversionResult result = await Conversion.ToMp4(Resources.MkvWithAudio, output).Start();

如果你想取得进步:

IConversion conversion = Conversion.ToMp4(Resources.MkvWithAudio, output);
conversion.OnProgress += (duration, length) => { currentProgress = duration; } 
await conversion.Start();

你好...我需要使用FFMPEG来转码从网页传来的流数据并将其发送到RTMP服务器。我在我的C# winform程序中有字节数组。我只需要转码并发送到RTMP服务器。我可以使用这个包装器来做到这一点吗?我曾经在Linux上使用socketio的nodejs服务器完成了这个任务。在那个平台上,我通过stdin发送二进制流,并在stderr中接收转换状态。我可以使用Xabe包装器来完成吗? - jstuardo

3

我一直在研究同样的事情,最初使用了MediaToolKit(在另一个答案中提到),它非常适合转换,但现在我需要更强大的东西。

一个看起来成熟且仍然活跃的选择是: https://github.com/hudl/HudlFfmpeg 您可以在这里阅读更多信息: http://public.hudl.com/bits/archives/2014/08/15/announcing-hudlffmpeg-a-c-framework-to-make-ffmpeg-interaction-simple/

另一个选项,可能不适用于许多情况,是直接从您的c#代码调用exe: http://www.codeproject.com/Articles/774093/Another-FFmpeg-exe-Csharp-Wrapper


3

我正在尝试使用一个叫做MediaHandler Pro的ffmpeg包装库,来自http://www.mediasoftpro.com,目前看起来很有希望。


这对你来说效果如何?另外,MediaHandler是否会作为一个进程生成ffmpeg.exe来完成其工作,还是有一个实际的P/Invoke库? - Glenn Slayden
我最终在几个项目中使用了它。在生产环境下,即使承受重负,它也表现良好。虽然我已经有一段时间没有使用它了,但从我记得的来看,它确实会作为一个进程生成ffmpeg.exe。 - Christophe Chang

2

给你... 这段代码大部分已经有两年以上的历史了,因此缺少很多异步操作,并且使用了过时的命名约定。在生产环境中运行了相当长的时间。 ~ JT

internal static class FFMpegArgUtils
    {
        public static string GetEncodeVideoFFMpegArgs(string sSourceFile, MP4Info objMp4Info, double nMbps, int iWidth, int iHeight, bool bIncludeAudio, string sOutputFile)
        {
            //Ensure file contains a video stream, otherwise this command will fail
            if (objMp4Info != null && objMp4Info.VideoStreamCount == 0)
            {
                throw new Exception("FFMpegArgUtils::GetEncodeVideoFFMpegArgs - mp4 does not contain a video stream");
            }

            int iBitRateInKbps = (int)(nMbps * 1000);


            StringBuilder sbArgs = new StringBuilder();
            sbArgs.Append(" -y -threads 2 -i \"" + sSourceFile + "\" -strict -2 "); // 0 tells it to choose how many threads to use

            if (bIncludeAudio == true)
            {
                //sbArgs.Append(" -acodec libmp3lame -ab 96k");
                sbArgs.Append(" -acodec aac -ar 44100 -ab 96k");
            }
            else
            {
                sbArgs.Append(" -an");
            }


            sbArgs.Append(" -vcodec libx264 -level 41 -r 15 -crf 25 -g 15  -keyint_min 45 -bf 0");

            //sbArgs.Append(" -vf pad=" + iWidth + ":" + iHeight + ":" + iVideoOffsetX + ":" + iVideoOffsetY);
            sbArgs.Append(String.Format(" -vf \"scale=iw*min({0}/iw\\,{1}/ih):ih*min({0}/iw\\,{1}/ih),pad={0}:{1}:({0}-iw)/2:({1}-ih)/2\"",iWidth, iHeight));

            //Output File
            sbArgs.Append(" \"" + sOutputFile + "\"");
            return sbArgs.ToString();
        }

        public static string GetEncodeAudioFFMpegArgs(string sSourceFile, string sOutputFile)
        {
            var args = String.Format(" -y -threads 2 -i \"{0}\" -strict -2  -acodec aac -ar 44100 -ab 96k -vn \"{1}\"", sSourceFile, sOutputFile);
            return args;


            //return GetEncodeVideoFFMpegArgs(sSourceFile, null, .2, 854, 480, true, sOutputFile);
            //StringBuilder sbArgs = new StringBuilder();
            //int iWidth = 854;
            //int iHeight = 480;
            //sbArgs.Append(" -y -i \"" + sSourceFile + "\" -strict -2 "); // 0 tells it to choose how many threads to use
            //sbArgs.Append(" -acodec aac -ar 44100 -ab 96k");
            //sbArgs.Append(" -vcodec libx264 -level 41 -r 15 -crf 25 -g 15  -keyint_min 45 -bf 0");
            //sbArgs.Append(String.Format(" -vf \"scale=iw*min({0}/iw\\,{1}/ih):ih*min({0}/iw\\,{1}/ih),pad={0}:{1}:({0}-iw)/2:({1}-ih)/2\"", iWidth, iHeight));
            //sbArgs.Append(" \"" + sOutputFile + "\"");
            //return sbArgs.ToString();
        }
    }

internal class CreateEncodedVideoCommand : ConsoleCommandBase
    {
        public event ProgressEventHandler OnProgressEvent;

        private string _sSourceFile;
        private  string _sOutputFolder;
        private double _nMaxMbps;

        public double BitrateInMbps
        {
            get { return _nMaxMbps; }
        }

        public int BitrateInKbps
        {
            get { return (int)Math.Round(_nMaxMbps * 1000); }
        }

        private int _iOutputWidth;
        private int _iOutputHeight;

        private bool _bIsConverting = false;
        //private TimeSpan _tsDuration;
        private double _nPercentageComplete;
        private string _sOutputFile;
        private string _sOutputFileName;


        private bool _bAudioEnabled = true;
        private string _sFFMpegPath;
        private string _sExePath;
        private string _sArgs;
        private MP4Info _objSourceInfo;
        private string _sOutputExt;

        /// <summary>
        /// Encodes an MP4 to the specs provided, quality is a value from 0 to 1
        /// </summary>
        /// <param name="nQuality">A value from 0 to 1</param>
        /// 
        public CreateEncodedVideoCommand(string sSourceFile, string sOutputFolder, string sFFMpegPath, double nMaxBitrateInMbps, MP4Info objSourceInfo, int iOutputWidth, int iOutputHeight, string sOutputExt)
        {
            _sSourceFile = sSourceFile;
            _sOutputFolder = sOutputFolder;
            _nMaxMbps = nMaxBitrateInMbps;
            _objSourceInfo = objSourceInfo;
            _iOutputWidth = iOutputWidth;
            _iOutputHeight = iOutputHeight;
            _sFFMpegPath = sFFMpegPath;
            _sOutputExt = sOutputExt;
        }

        public void SetOutputFileName(string sOutputFileName)
        {
            _sOutputFileName = sOutputFileName;
        }


        public override void Execute()
        {
            try
            {
                _bIsConverting = false;

                string sFileName = _sOutputFileName != null ? _sOutputFileName : Path.GetFileNameWithoutExtension(_sSourceFile) + "_" + _iOutputWidth + "." + _sOutputExt;
                _sOutputFile = _sOutputFolder + "\\" + sFileName;

                _sExePath = _sFFMpegPath;
                _sArgs = FFMpegArgUtils.GetEncodeVideoFFMpegArgs(_sSourceFile, _objSourceInfo,_nMaxMbps, _iOutputWidth, _iOutputHeight, _bAudioEnabled, _sOutputFile);

                InternalExecute(_sExePath, _sArgs);
            }
            catch (Exception objEx)
            {
                DispatchException(objEx);
            }
        }

        public override string GetCommandInfo()
        {
            StringBuilder sbInfo = new StringBuilder();
            sbInfo.AppendLine("CreateEncodeVideoCommand");
            sbInfo.AppendLine("Exe: " + _sExePath);
            sbInfo.AppendLine("Args: " + _sArgs);
            sbInfo.AppendLine("[ConsoleOutput]");
            sbInfo.Append(ConsoleOutput);
            sbInfo.AppendLine("[ErrorOutput]");
            sbInfo.Append(ErrorOutput);

            return base.GetCommandInfo() + "\n" + sbInfo.ToString();
        }

        protected override void OnInternalCommandComplete(int iExitCode)
        {
            DispatchCommandComplete( iExitCode == 0 ? CommandResultType.Success : CommandResultType.Fail);
        }

        override protected void OnOutputRecieved(object sender, ProcessOutputEventArgs objArgs)
        {
            //FMPEG out always shows as Error
            base.OnOutputRecieved(sender, objArgs);

            if (_bIsConverting == false && objArgs.Data.StartsWith("Press [q] to stop encoding") == true)
            {
                _bIsConverting = true;
            }
            else if (_bIsConverting == true && objArgs.Data.StartsWith("frame=") == true)
            {
                //Capture Progress
                UpdateProgressFromOutputLine(objArgs.Data);
            }
            else if (_bIsConverting == true && _nPercentageComplete > .8 && objArgs.Data.StartsWith("frame=") == false)
            {
                UpdateProgress(1);
                _bIsConverting = false;
            }
        }

        override protected void OnProcessExit(object sender, ProcessExitedEventArgs args)
        {
            _bIsConverting = false;
            base.OnProcessExit(sender, args);
        }

        override public void Abort()
        {
            if (_objCurrentProcessRunner != null)
            {
                //_objCurrentProcessRunner.SendLineToInputStream("q");
                _objCurrentProcessRunner.Dispose();
            }
        }

        #region Helpers

        //private void CaptureSourceDetailsFromOutput()
        //{
        //    String sInputStreamInfoStartLine = _colErrorLines.SingleOrDefault(o => o.StartsWith("Input #0"));
        //    int iStreamInfoStartIndex = _colErrorLines.IndexOf(sInputStreamInfoStartLine);
        //    if (iStreamInfoStartIndex >= 0)
        //    {
        //        string sDurationInfoLine = _colErrorLines[iStreamInfoStartIndex + 1];
        //        string sDurantionTime = sDurationInfoLine.Substring(12, 11);

        //        _tsDuration = VideoUtils.GetDurationFromFFMpegDurationString(sDurantionTime);
        //    }
        //}

        private void UpdateProgressFromOutputLine(string sOutputLine)
        {
            int iTimeIndex = sOutputLine.IndexOf("time=");
            int iBitrateIndex = sOutputLine.IndexOf(" bitrate=");

            string sCurrentTime = sOutputLine.Substring(iTimeIndex + 5, iBitrateIndex - iTimeIndex - 5);
            double nCurrentTimeInSeconds = double.Parse(sCurrentTime);
            double nPercentageComplete = nCurrentTimeInSeconds / _objSourceInfo.Duration.TotalSeconds;

            UpdateProgress(nPercentageComplete);
            //Console.WriteLine("Progress: " + _nPercentageComplete);
        }

        private void UpdateProgress(double nPercentageComplete)
        {
            _nPercentageComplete = nPercentageComplete;
            if (OnProgressEvent != null)
            {
                OnProgressEvent(this, new ProgressEventArgs( _nPercentageComplete));
            }
        }

        #endregion

        //public TimeSpan Duration { get { return _tsDuration; } }

        public double Progress { get { return _nPercentageComplete;  } }
        public string OutputFile { get { return _sOutputFile; } }

        public bool AudioEnabled
        {
            get { return _bAudioEnabled; }
            set { _bAudioEnabled = value; }
        }
}

public abstract class ConsoleCommandBase : CommandBase, ICommand
    {
        protected ProcessRunner _objCurrentProcessRunner;
        protected   List<String> _colOutputLines;
        protected List<String> _colErrorLines;


        private int _iExitCode;

        public ConsoleCommandBase()
        {
            _colOutputLines = new List<string>();
            _colErrorLines = new List<string>();
        }

        protected void InternalExecute(string sExePath, string sArgs)
        {
            InternalExecute(sExePath, sArgs, null, null, null);
        }

        protected void InternalExecute(string sExePath, string sArgs, string sDomain, string sUsername, string sPassword)
        {
            try
            {
                if (_objCurrentProcessRunner == null || _bIsRunning == false)
                {
                    StringReader objStringReader = new StringReader(string.Empty);

                    _objCurrentProcessRunner = new ProcessRunner(sExePath, sArgs);

                    _objCurrentProcessRunner.SetCredentials(sDomain, sUsername, sPassword);

                    _objCurrentProcessRunner.OutputReceived += new ProcessOutputEventHandler(OnOutputRecieved);
                    _objCurrentProcessRunner.ProcessExited += new ProcessExitedEventHandler(OnProcessExit);
                    _objCurrentProcessRunner.Run();

                    _bIsRunning = true;
                    _bIsComplete = false;
                }
                else
                {
                    DispatchException(new Exception("Processor Already Running"));
                }
            }
            catch (Exception objEx)
            {
                DispatchException(objEx);
            }
        }

        protected virtual void OnOutputRecieved(object sender, ProcessOutputEventArgs args)
        {
            try
            {
                if (args.Error == true)
                {
                    _colErrorLines.Add(args.Data);
                    //Console.WriteLine("Error: " + args.Data);
                }
                else
                {
                    _colOutputLines.Add(args.Data);
                    //Console.WriteLine(args.Data);
                }
            }
            catch (Exception objEx)
            {
                DispatchException(objEx);
            }
        }

        protected virtual void OnProcessExit(object sender, ProcessExitedEventArgs args)
        {
            try
            {
                Console.Write(ConsoleOutput);
                _iExitCode = args.ExitCode;

                _bIsRunning = false;
                _bIsComplete = true;

                //Some commands actually fail to succeed
                //if(args.ExitCode != 0)
                //{
                //    DispatchException(new Exception("Command Failed: " + this.GetType().Name + "\nConsole: " + ConsoleOutput + "\nConsoleError: " + ErrorOutput));
                //}

                OnInternalCommandComplete(_iExitCode);

                if (_objCurrentProcessRunner != null)
                {
                    _objCurrentProcessRunner.Dispose();
                    _objCurrentProcessRunner = null;    
                }
            }
            catch (Exception objEx)
            {
                DispatchException(objEx);
            }
        }

        abstract protected void OnInternalCommandComplete(int iExitCode);

        protected string JoinLines(List<String> colLines)
        {
            StringBuilder sbOutput = new StringBuilder();
            colLines.ForEach( o => sbOutput.AppendLine(o));
            return sbOutput.ToString();
        }

        #region Properties
        public int ExitCode
        {
            get { return _iExitCode; }
        }
        #endregion

        public override string GetCommandInfo()
        {
            StringBuilder sbCommandInfo = new StringBuilder();
            sbCommandInfo.AppendLine("Command:  " + this.GetType().Name);
            sbCommandInfo.AppendLine("Console Output");
            if (_colOutputLines != null)
            {
                foreach (string sOutputLine in _colOutputLines)
                {
                    sbCommandInfo.AppendLine("\t" + sOutputLine);
                }
            }
            sbCommandInfo.AppendLine("Error Output");
            if (_colErrorLines != null)
            {
                foreach (string sErrorLine in _colErrorLines)
                {
                    sbCommandInfo.AppendLine("\t" + sErrorLine);
                }
            }
            return sbCommandInfo.ToString();
        }

        public String ConsoleOutput { get { return JoinLines(_colOutputLines); } }
        public String ErrorOutput { get { return JoinLines(_colErrorLines);} }

    }

CommandBase : ICommand
    {
        protected IDedooseContext _context;
        protected Boolean _bIsRunning = false;
        protected Boolean _bIsComplete = false;

        #region Custom Events
        public event CommandCompleteEventHandler OnCommandComplete;
        event CommandCompleteEventHandler ICommand.OnCommandComplete
        {
            add { if (OnCommandComplete != null) { lock (OnCommandComplete) { OnCommandComplete += value; } } else { OnCommandComplete = new CommandCompleteEventHandler(value); } }
            remove { if (OnCommandComplete != null) { lock (OnCommandComplete) { OnCommandComplete -= value; } } }
        }

        public event UnhandledExceptionEventHandler OnCommandException;
        event UnhandledExceptionEventHandler ICommand.OnCommandException
        {
            add { if (OnCommandException != null) { lock (OnCommandException) { OnCommandException += value; } } else { OnCommandException = new UnhandledExceptionEventHandler(value); } }
            remove { if (OnCommandException != null) { lock (OnCommandException) { OnCommandException -= value; } } }
        }

        public event ProgressEventHandler OnProgressUpdate;
        event ProgressEventHandler ICommand.OnProgressUpdate
        {
            add { if (OnProgressUpdate != null) { lock (OnProgressUpdate) { OnProgressUpdate += value; } } else { OnProgressUpdate = new ProgressEventHandler(value); } }
            remove { if (OnProgressUpdate != null) { lock (OnProgressUpdate) { OnProgressUpdate -= value; } } }
        }
        #endregion

        protected CommandBase()
        {
            _context = UnityGlobalContainer.Instance.Context;
        }

        protected void DispatchCommandComplete(CommandResultType enResult)
        {
            if (enResult == CommandResultType.Fail)
            {
                StringBuilder sbMessage = new StringBuilder();
                sbMessage.AppendLine("Command Commpleted with Failure: "  + this.GetType().Name);
                sbMessage.Append(GetCommandInfo());
                Exception objEx = new Exception(sbMessage.ToString());
                DispatchException(objEx);
            }
            else
            {
                if (OnCommandComplete != null)
                {
                    OnCommandComplete(this, new CommandCompleteEventArgs(enResult));
                }
            }
        }

        protected void DispatchException(Exception objEx)
        {
            if (OnCommandException != null)
            { 
                OnCommandException(this, new UnhandledExceptionEventArgs(objEx, true)); 
            }
            else
            {
                _context.Logger.LogException(objEx, MethodBase.GetCurrentMethod());
                throw objEx;
            }
        }

        protected void DispatchProgressUpdate(double nProgressRatio)
        {
            if (OnProgressUpdate != null) { OnProgressUpdate(this, new ProgressEventArgs(nProgressRatio)); } 
        }

        public virtual string GetCommandInfo()
        {
            return "Not Implemented: " + this.GetType().Name;
        }

        public virtual void Execute() { throw new NotImplementedException(); }
        public virtual void Abort() { throw new NotImplementedException(); }

        public Boolean IsRunning { get { return _bIsRunning; } }
        public Boolean IsComplete { get { return _bIsComplete; } }

        public double GetProgressRatio()
        {
            throw new NotImplementedException();
        }
    }

public delegate void CommandCompleteEventHandler(object sender, CommandCompleteEventArgs e);

    public interface ICommand
    {
        event CommandCompleteEventHandler OnCommandComplete;
        event UnhandledExceptionEventHandler OnCommandException;
        event ProgressEventHandler OnProgressUpdate;

        double GetProgressRatio();
        string GetCommandInfo();

        void Execute();
        void Abort();
    }

// 如果需要处理运行程序,请查找Roger Knapp的ProcessRunner


2

1
谢谢提供链接,但据我所见,您的代码是用Java写的,而不是C#。 - Jacob Poul Richardt
嗨lillemanden,我给出的链接实际上是用Java实现的,如果你下载文章底部的zip文件,你会看到里面有一个jar归档文件。谢谢,Ilya - Ilya
答案中的链接似乎失效了:“无法访问此网站 - ivolo.mit.edu 响应时间过长。” - Pang

1
        string result = String.Empty;
        StreamReader srOutput = null;
        var oInfo = new ProcessStartInfo(exePath, parameters)
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };

        var output = string.Empty;

        try
        {
            Process process = System.Diagnostics.Process.Start(oInfo);
            output = process.StandardError.ReadToEnd();
            process.WaitForExit();
            process.Close();
        }
        catch (Exception)
        {
            output = string.Empty;
        }
        return output;

这个包装器不会让方法陷入循环中。 试试这个,对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接