【问题标题】:how to handle winrar diagnostic messages from code如何处理来自代码的winrar诊断消息
【发布时间】:2013-06-14 11:19:24
【问题描述】:

我正在该应用程序中开发一个 Windows 应用程序,我使用 winrar 命令行实用程序制作 rar 文件。

代码

public static string RarFiles(string rarPackagePath,
        Dictionary<int, string> accFiles)
    {
        string error = "";
        try
        {
            string[] files = new string[accFiles.Count];
            int i = 0;
            foreach (var fList_item in accFiles)
            {
                files[i] = "\"" + fList_item.Value;
                i++;
            }
            string fileList = string.Join("\" ", files);
            fileList += "\"";
            System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
            string cmdArgs = string.Format("A {0} {1} -ep1 -r",
                String.Format("\"{0}\"", rarPackagePath),
                fileList);
            sdp.ErrorDialog = true;
            sdp.UseShellExecute = true;
            sdp.Arguments = cmdArgs;
            sdp.FileName = winrarPath;//Winrar.exe path
            sdp.CreateNoWindow = false;
            sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
            process.WaitForExit();
            //string s = process.StandardOutput.ReadToEnd();
            error = "OK";
        }
        catch (Exception ex)
        {
            error = ex.Message;
        }
        return error;
    }

谁能告诉我如何处理winrar诊断信息。

【问题讨论】:

  • 为什么要注释掉string s中保存标准输出的那一行?应该存储来自 winRar 的所有输出。
  • 因为字符串 s 总是空白。当使用 winrar.exe 制作 rar 时出现错误时,它会显示诊断消息并且在 process.StandardOutput.ReadToEnd() 中不返回任何内容;

标签: c# winforms process winrar


【解决方案1】:

我认为你错过了一些部分:

尝试添加这些:

sdp.StartInfo.RedirectStandardOutput = true;

在 Start 之后,添加一个字符串以获取输出。之后,调用 WaitForExit()

sdp.Start();
string output = stillc.StandardOutput.ReadToEnd();
sdp.WaitForExit();

*注意:这只有在控制台窗口中显示输出时才有效。

希望对你有帮助:)

【讨论】:

  • 为此我需要设置 sdp.UseShellExecute = false;显示可执行窗口。我不想显示那个窗口。顺便说一句,它仍然显示空白输出
猜你喜欢
  • 1970-01-01
  • 2021-08-12
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多