I am using the following code:
Process process = new Process();
ProcessStartInfo info = new ProcessStartInfo(#"java -jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");
process.StartInfo = info;
process.Start();
process.WaitForExit();
process.Dispose();
I get a Win32 exception:
The system cannot find the file specified
How can I fix this problem?
First argument of ProcessStartInfo constructor should be file name only. All arguments to application should be putted into second argument of ProcessStartInfo constructor:
new ProcessStartInfo("java", #"-jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");
Related
What I want is creating a new Winforms project using dotnet new, and I have tried the following code but not worked.
ProcessStartInfo psi = new ProcessStartInfo();
//psi.FileName = "cmd.exe";
//psi.Arguments = #"/C cd C:\Users\Administrator\Desktop\test & dotnet new winforms –n Test1";
psi.FileName = "dotnet.exe";
psi.Arguments = #"/C dotnet new winforms –-name Test3";
Process p = Process.Start(psi);
In addition, how to specify the location of the new project, such as C:\Users\Administrator\Desktop\test?
Try the following:
Process p = Process.Start("dotnet", "new winforms --name Test3 --output C:\\Users\\Administrator\\Desktop\\test");
Process.Start with arguments Documentation
Dotnet new documentation for the -o |--output flag
Also make sure that you are running with the right permissions to access the folder.
I am trying to send commands to RDP connections from a C# console application using PsExec, this is the command
PsExec.exe //1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe
wich works perfectly, runs RemoteAppExe.exe on that system, the problem is that within C# it dosen't work, here is my code :
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"PsExec.exe";
pProcess.StartInfo.Arguments = "//1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
What is the problem? I assume it might be the fact that arguments are not properly escaped.
Any ideeas?
Thanks.
It should be:
pProcess.StartInfo.Arguments = #"\\1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe";
Changing the argument line made the code work for me.
I have a php code that calls ffmpeg from the windows command line (because I run apache locally) with:
//ffmpeg -i input.mp4 -r 20 output.gif
echo exec('C:\ffmpeg\bin\ffmpeg.exe -i videos/file.avi -r 10 -s 450x230 videos/file.gif');
And it works fine! Now I wrote a C# program and I try to do the same thing with:
System.Diagnostics.Process.Start
but it fails.
Question: How do I execute the same command from the PHP code but from inside a C# code?
Process process = Process.Start(
new ProcessStartInfo
{
FileName = #"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = "-i videos/file.avi -r 10 -s 450x230 videos/file.gif",
WindowStyle = ProcessWindowStyle.Hidden
}
);
process.WaitForExit();
If it fails an exception will be raised, e.g.
System.ComponentModel.Win32Exception with HResult=-2147467259 if the executable wasn't found.
I am trying to Run DelTemp-Final.vbs on a remote server which is being imported from a web portal.
I tried to use native C# to get the task done, No luck. So I opted for PS tools to help me out. I am stuck with he below scenario. Please help
Psexec is working using command prompt, but not working with c#
Below is the command prompt output which is working fine:
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit1 -u XXXX\xxxxxxx -p xxxx
Xxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit1 with process ID 5452.
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit2 -u XXXX\xxxxxxx -p xxxxx
xxxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit2 with process ID 7416.
Whereas, the same command does not work when included with c# in asp.net
Process p=new Process();
p.StartInfo.UseShellExecute=false;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardError=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName=#"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments="\\\\"+li.Text+" -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.Start();
string output=p.StandardOutput.ReadToEnd();
string errormessage=p.StandardError.ReadToEnd();
p.WaitForExit();
txtValueA.Text +="PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text +="<br/> Output : " + output+"| error messsage: "+errormessage;
Output of the above code for two servers is as follows:
PSexec argument :\\testusit1 -u XXXX\xxxxxxxxxx -p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com The handle is invalid. Connecting to testusit1... Starting
PSEXESVC service on testusit1... Connecting with PsExec service on testusit1... Error deriving
session key:
PSexec argument :\\testusit2 -u XXXX\xxxxxxxxxxx-p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com Access is denied. Connecting to testusit2... Starting PSEXESVC
service on testusit2... Could not start PSEXESVC service on testusit2: Connecting to testusit2...
Starting PSEXESVC service on testusit2...
Please assist.. Is there any changes I need to do to the script? Any help is greatly appreciated.
Thanks a lot in advance :)
Try directing your output to an event handler instead. I just ran a variation of this on my server with psexec executing ipconfig on the remote machine and I was able to view all output.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments = "\\\\" + li.Text + " -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
txtValueA.Text += "PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text += "<br/> Output : " + output + "| error messsage: " + errormessage;
}
public static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
//Standard output event handler for PSEXEC
public static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
I am using the following code to run a Linux console command via Mono in a C# application:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
This works as expected. But, if i give the command as "-c ls -l" or "-c ls /path" I still get the output with the -l and path ignored.
What syntax should I use in using multiple switches for a command?
You forgot to quote the command.
Did you try the following on the bash prompt ?
bash -c ls -l
I strongly suggest to read the man bash.
And also the getopt manual as it's what bash use to parse its parameters.
It has exactly the same behavior as bash -c ls
Why? Because you have to tell bash that ls -l is the full argument of -c, otherwise -l is treated like an argument of bash.
Either bash -c 'ls -l' or bash -c "ls -l" will do what you expect.
You have to add quotes like this:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c 'ls -l'");