首页 > 编程语言 >C#调用第三方软件及交互

C#调用第三方软件及交互

时间:2024-09-02 17:47:45浏览次数:7  
标签:调用 C# await process Task var writer 交互

最近有一个功能pdf转epub,网上找到一个解决方案pdf2epubEX,Windows环境只能以docker方式安装,手动执行docker命令是可以的,如下图:

由于pdf数量过多,全部手动转肯定不行,想通过C#调用docker命令,但是这种方法会报错"the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty’
",网上查了一下是Windows和docker的交互有关系。后面选择了在Linux上面直接安装软件,然后用C#调用软件并且提供交互。
需要与软件进行交互要用异步,不然streamReader.ReadLine()当输出完以后,有交互的话会卡在这里,一开始这里也琢磨了好久,这也是为什么要写这篇文章的原因吧。

以下是相关代码:

static async Task Main(string[] args)
{
    string folderPath = Path.Combine("pdf");
    string[] pdfFiles = Directory.GetFiles(folderPath, "*.pdf");
    foreach (var item in pdfFiles)
    {
        try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "pdf2epubEX",
                Arguments = item,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            var filename = Path.GetFileName(item);
            Console.WriteLine($"开始处理:{filename}");
            using (var process = new Process { StartInfo = startInfo })
            {
                process.Start();

                // 处理标准输出流
                var outputTask = ReadOutputAsync(process.StandardOutput);
                // 处理标准错误流
                var errorTask = ReadOutputAsync(process.StandardError);

                // 写入标准输入流
                await WriteInputAsync(process.StandardInput);

                // 等待进程完成
                await process.WaitForExitAsync();

                // 等待输出和错误任务完成
                await Task.WhenAll(outputTask, errorTask);

            }
        }
        catch (Exception ex)
        {
            
        }
        finally
        {

        }

    }
    
}

static async Task WriteInputAsync(StreamWriter standardInput)
{
    using (var writer = standardInput)
    {
        // 与软件进行交互, 这里可以发送多个输入
        await writer.WriteLineAsync("n");
        await writer.WriteLineAsync("svg");
        await writer.WriteLineAsync();//这里类似于直接敲回车,但是对于默认的参数不加这句好像也可以
        // 结束输入流
        writer.Close();
    }
}

static async Task ReadOutputAsync(StreamReader reader)
{
    using (var streamReader = reader)
    {
        string line;
        while ((line = await streamReader.ReadLineAsync()) != null)
        {
            Console.WriteLine("Output: " + line);
        }
    }
}

标签:调用,C#,await,process,Task,var,writer,交互
From: https://www.cnblogs.com/dx5800/p/18393167

相关文章

  • Java 根据模板生成 PDF 文件 以及 excel 文件
    模板PDF的字段引入maven<!--iTextforgeneratingPDFfiles--><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.2</v......
  • 33 CONST
    CONSTReference:CONSTinc++Ifyoudecalresomethingwithconst,you'resayingthatyouarenotgoingtomodifythatthing.Combningwithpointersconstint*a=newint;//orintconst*a=newint;//theymeanthatyoucannotmodifytheco......
  • Centos7 离线安装字体库
    一、获取依赖包1、准备另一台能联网的Linux机器(要配置yum,建议配阿里的,网上很多教程,很简单!)2、安装获取依赖包的命令,然后新建一个文件来存放依赖包#安装获取依赖包的命令yum-yinstallyum-utils#新建一个文件来存放依赖包mkdir-p/root/font_rpm#执行获取相关依赖命令yumdo......
  • 注解和反射process6
    1、注解(Annotation)一些方法加了一杠表示不推荐使用,但是也能用写代码时标了黄块的代码体就是警告1.1注解注解(Annotation)——用@符号标示不是程序本身,可以对程序作出解释(这一点与注释(comment)没什么区别)可以被其他程序(比如:编译器)读取通过反射机制编程实现对辅助信息数据的......
  • CAMEL:通过角色扮演,实现大模型自主合作的多智能体框架
    CAMEL:通过角色扮演,实现大模型自主合作的多智能体框架提出背景秒懂大纲解法拆解类比全流程分析CAMEL对比AutoGen、MetaGPT为什么通过角色扮演和对话,就能实现大模型的自主合作,探索自主交互智能体在复杂任务求解中的潜力? 论文:CAMEL:CommunicativeAgentsfor“Mind”Explorati......
  • 电子秤方案的主控芯片选型SIC8833
     电子秤的方案设计功能比较简单,四个压力模块和ADC芯片以及再加个主控芯片大约就构成了其核心功能的器件要求。ADC芯片的功能是将压力模块所得到压力值转化为可显示的数值,在通过LED或者LCD屏展现出来,就是后面我们测量物品或体重所得到的重量斤数。 主控芯片就是MCU,它是方案的......
  • WPF communicate across different modules via event
    //Runtimeproject,cclasslibraryusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Input;namespaceRuntime{publicclassDelCmd:......
  • C# 定时任务
    usingSystem.Timers;namespacePipette.Tool{internalclassTimerTool{publicstaticvoidScheduledTask(intinterval,ElapsedEventHandlerelapsed){System.Timers.Timertimer=newSystem.Timers.Timer();......
  • 五轴模型RTCP视觉标定原理
            ......
  • [vue] jszip html-docx-js file-saver 图片,纯文本 ,打包压缩,下载跨域问题
    npminstalljszipfile-saverimportJSZipfrom'jszip';importFileSaverfrom'file-saver';JSZip创建JSZip实例:constzip=newJSZip();创建文件:支持导出纯文本zip.file("hello.txt","HelloWorld\n");创建文件夹:zip.folder("file")......