【问题标题】:using C# Process to run a Executable program使用 C# Process 运行可执行程序
【发布时间】:2011-11-11 13:08:44
【问题描述】:

我是一名生物信息学家,我在工作中使用 C#。我已经多次使用 C# 中的进程来运行可执行程序。这次我有一个新问题。我在 Windows 中为名为 Blast(http://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download) 的程序下载了一个 exe 文件。如果我输入我的命令:

blastp -query input.txt -db pdbaa -out output.txt

它工作正常。但是当我从记事本复制粘贴命令时,它会出错。我搜索了这个问题,发现它是由复制和粘贴引起的“UTF-8 与 ISO-latin 的编码问题”(http://biostar.stackexchange.com/questions/7997/an-error-by-using-ncbi-blast-2-2-25-on-windows)。

现在我想从 c# 运行进程来调用 exe 文件,我遇到了同样的问题,我猜这是因为该进程执行了复制和粘贴之类的操作。这是我的代码:

 public void Calculate()
    {
        Process proc = new Process();
        proc.StartInfo.WorkingDirectory = Program.NCBIBlastDirectory;
        proc.StartInfo.FileName = @"C:\Program Files\NCBI\blast-2.2.25+\bin\blastp.exe";
        proc.StartInfo.Arguments = "blastp -query input.txt -db pdbaa -out output.txt";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.Start();
        proc.WaitForExit();
        proc.Close();
    }

你知道我该如何解决这个问题吗?

提前致谢。

【问题讨论】:

  • 如果您也从记事本复制并粘贴了您的参数,那么它们可能保留了编码。在记事本中保存带有 ansi 编码的文本文件,然后复制这些参数。
  • 您确定应该将“blastp”作为参数中的第一个单词吗?这不是exe名称吗?该进程现在将调用此命令:C:\Program Files\NCBI\blast-2.2.25+\bin\blastp.exe blastp -query input.txt -db pdbaa -out output.txt

标签: c# process executable


【解决方案1】:

我可以看到的一个问题是在您设置参数的行中:

proc.StartInfo.Arguments = "blastp -query input.txt -db pdbaa -out output.txt";

我想你的意思是:

proc.StartInfo.Arguments = "-query input.txt -db pdbaa -out output.txt";

因此您无需在参数中再次指定可执行文件名称 - 这就是 FileName 的用途。

另一件事是,如果您不使用 shell-execute 来启动它们,很多应用程序的行为就会不太好。首先使用 shell-execute 尝试(显然不重定向任何 std*),如果它以这种方式工作,那么您就会知道问题所在 - 尽管我担心您对此无能为力。

还有,为什么是行

proc.StartInfo.RedirectStandardError = true;

重复两次?

【讨论】:

  • 谢谢。是的,问题出在 Arguments 语句中。我删除了 dthe blastp,现在正在工作。
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多