using C# Process to run a Executable program

我怕爱的太早我们不能终老 提交于 2019-11-30 19:08:30

问题


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.


回答1:


One problem I can see is in the line where you set the Arguments:

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

I think you meant:

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

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

proc.StartInfo.RedirectStandardError = true;

repeated twice?



来源:https://stackoverflow.com/questions/8094428/using-c-sharp-process-to-run-a-executable-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!