使用 C# Process 运行可执行程序
我是一名生物信息学人员,我的工作使用 C#。我已经多次使用 C# 中的进程来运行可执行程序。这次我有一个新问题。我在 Windows 中下载了一个名为 Blast 的程序的 exe 文件(http://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=下载)。如果我输入我的命令:
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();
}
您知道我该如何解决这个问题吗?
提前致谢。
I am a Bioinformatic person and I use C# for my work. I have been using Processes in C# to run Executable programs several times. This time I have a new issue. I have downloaded an exe file in Windows for a program named Blast(http://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download). If I type in my command which is :
blastp -query input.txt -db pdbaa -out output.txt
it works fine. But when I copy paste the command from a notepad it will give an error. I searched for the problem and I found that it is an "encoding problem UTF-8 versus ISO-latin" (http://biostar.stackexchange.com/questions/7997/an-error-by-using-ncbi-blast-2-2-25-on-windows) which is caused by copy and paste.
Now that I want to run the process from c# to call the exe file I get the same problem and I guess it is because the process does something like copy and paste. Here is my code:
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();
}
Do you have any idea how I can solve this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看到的一个问题是在您设置参数的行中:
我认为您的意思是:
所以您不需要在参数中再次指定可执行文件名称 - 这就是 FileName 的用途。
另一件事是,有很多应用程序如果不使用 shell-execute 来启动它们,它们的行为就不会很好。首先使用 shell-execute 尝试一下(显然没有重定向任何 std*),如果它以这种方式工作,那么你就会知道问题是什么 - 尽管我担心你对此无能为力。
另外,为什么该行
重复两次?
One problem I can see is in the line where you set the Arguments:
I think you meant:
So you don't need to specify the executable name again in the Arguments - that's what FileName is for.
The other thing is that there are a lot of applications which don't behave too well if you don't use shell-execute to start them. Try it first with shell-execute (and obviously without redirecting any std*), and if it works that way, then you'll know what the issue is - although I'm afraid there's not much you can do about it.
Also, why is the line
repeated twice?