I'm trying to create a ASP.NET web api to trigger a crawl event to happen. I can't seem to get cygwin to process any of the commands I give it. The only thing I can really do is get it to open a terminal. Once the terminal is open I'd have to redirect the pwd to another location and then trigger my command I want.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = false;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.FileName = "C:\\cygwin64\\bin\\mintty.exe";
p.StartInfo = info;
p.Start();
StreamWriter sw = p.StandardInput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"cd C:\Users\UName\Desktop\apache-nutch-2.3-mongodb\runtime\local\");
sw.WriteLine("bin/autoCrawl");
}
sw.Close();
p.WaitForExit();
I've tried many approaches, this is the last one I've tried but it just does nothing. Is there a way to launch this crawl from my .NET application? I've looked into the NutchApi about creating a new job with a type of crawl but I'm not sure if that applies here or not.
I ended up figuring out how to use the NutchApi to answer my question.
Related
I have a command line application that calls a 'netsh' process and changes some IP information. The problem that I have is that every time I call my app in CMD or PowerShell, it starts a new instance of CMD(opens a new CMD window and closes it after it's finished executing)
I would like to know if there is a way to have everything happen in the same window
here is the code that starts a process:
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
p.StartInfo = psi;
p.Start();
Console.WriteLine("netsh query string is: " + "***" + queryStr + "***");
This is my first question, please don't judge too harshly
I hope I understood your question correctly. You could hide the Shell, while redirecting the ouput. For example,
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
p.StartInfo = psi;
p.Start();
var output = p.StandardOutput.ReadToEnd();
ProcessStartInfo.WindowStyle allows to set the state of Window when the process starts. In the current scenario, you could set it to ProcessWindowStyle.Hidden
ProcessStartInfo.UseShellExecute indicates whether to use the OS's Shell. Disabling this would help in redirecting the output.
ProcessStartInfo.RedirectStandardOutput indicates whether the output is written to StandardOutput. By setting it to true, you can redirect the output stream and use the Process.StandardOutput to read the output and display as per application design
I encountered some problems during writing application to shutdown/restart linux from windows in c#.
I'm trying to use putty to do this task, but something is wrong.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "putty.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.Arguments = #"root#192.168.0.25 -pw 1234qwer ";
p.StartInfo = info;
p.Start();
p.StandardInput.WriteLine("shutdown -h ");
p.StandardInput.WriteLine("exit");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Above I've pasted code that I've used to achieve this goal.
Everything is fine till I have to write something more in putty command line, in this case StandardInput is not a good way to do this and I didn't find other way to do this.
I also tried to use PLINK.exe in the same way but it also didn't solve my problems - in fact PLINK does not even show up.
Any ideas or tips how to solve this problem would be great.
Try SSH.NET
using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
client.Connect();
client.RunCommand("shutdown -h now;systemctl poweroff");
client.Disconnect();
}
I am trying to run the below code and expect to get a file with the name abc.txt with Dir information. I get the Command prompt but the commands are not running.
There are lot's of questions in StackOverFlow which discuss this, but nothing mentioned there is either required or works.
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
if (p.StandardInput.BaseStream.CanWrite)
{
p.StandardInput.WriteLine("dir >>c:\abc.txt");
}
You have to escape your "\" in the sw.WriteLine for c:\
just add a # before like this:
sw.WriteLine(#"dir >>c:\abc.txt");
Consider: You may have to run aour application under an elevated administrative context to write to c:\abc.txt
:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine(#"cd" +#"program files\mysoftwarefolder");
sw.WriteLine(#"mysoftware.exe" +#"d:\myfolder\file1.xxx" +#"d:\myfolder\file2.xxx" +#"-mycommand");
sw.WriteLine(#"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
Write these commands in a batch file and execute the batch file.
In batch file:
cd c:\program files\mysoftwareFolder
mysoftware.exe
d:\myfolder\file1.xxx
d:\myfolder\file2.xxx -mycommand
exit
Code:
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "path to batchfile.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
Instead of:
startinfo.FileName = "cmd";
Directly use
startinfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
Then pass the arguments to the start info as
startinfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
So the whole code looks like:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
p.StartInfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you need to see output from your program you can simply use the output string.
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
echo on
c:
cdprogram files\mysoftware folder
mysoftware.exed:\myfolder\file1.xxx
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so:
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine("cd \"program files\\mysoftwarefolder\"");
sw.WriteLine("mysoftware.exe \"d:\\myfolder\\file1.xxx\" d:\\myfolder\\file2.xxx\" -mycommand");
sw.WriteLine(#"exit");
I've some troubles with running processes and passing args to them.
I know how to run process with some args
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");
Process p = Process.Start(psi)
The problem is that after script is executed process is terminated. That's why there is "/c"
But I'm running multiple scripts and I would like to run them in one process ("cmd.exe") not to start new process every time.
Is there some solutions for it ?
I hope somebody understand what I'm talking about ;)
I recommend you utilize a batch file to script the execution of your executables and call your batch file instead. Or, you can do this -
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = new StreamWriter(p.StandardInput))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
It sounds like you ought to investigate redirecting the standard input - be sure to also set psi.UseShellExecute to false. You'll probably also want to redirect standard output, so you can have some way of knowing what your child process is doing.
Read more about redirection here.