Run "ping" command when link button is clicked - c#

I am trying to pop up a Windows Command prompt and run a ping command when a link button is clicked. Link button looks something like:
<asp:LinkButton runat="server" ID="lbFTPIP" OnCommand="lbFTPIP_OnCommand" CommandArgumnet="1.2.3.4" Text="1.2.3.4"/>
I tried this for OnCommand:
protected void lbFTPIP_OnCommand(object sender, CommandEventArgs e)
{
string sFTPIP = e.CommandArgument.ToString();
string sCmdText = #"ping -a " + sFTPIP;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = sCmdText;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
}
When I click the link, it opens the command prompt but does not display or execute the command, it just shows the current directory. Not sure what I am missing here.
It is part of a web page, if this makes a difference.

In order to open a console and immediately run a command, you need to use either the /C or /K switch:
// Will run the command and then close the console.
string sCmdText = #"/C ping -a " + sFTPIP;
// Will run the command and keep the console open.
string sCmdText = #"/K ping -a " + sFTPIP;
If you want to institute a "Press any key", you can add a PAUSE:
// Will run the command, wait for the user press a key, and then close the console.
string sCmdText = #"/C ping -a " + sFTPIP + " & PAUSE";
EDIT:
It would probably be best to redirect your output and then display the results separately:
Process p = new Process();
// No need to use the CMD processor - just call ping directly.
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments = "-a " + sFTPIP;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
var output = p.StandardOutput.ReadToEnd();
// Do something the output.

You don't need to execute cmd.exe, just execute ping.exe instead.
string sCmdText = #"-a " + sFTPIP;
Process p = new Process();
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments = sCmdText;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
Also, don't set UseShellExecute = false unless you intend to redirect the output, which I'm surprised you are not doing.

First thing is that you have something strange here CommandArgumnet="1.2.3.4" that is misspelled. The other thing is the /C and a space before ping.

Related

C# Start Process on Mac - FFMPEG - Exit Code 1

I am trying to start a process on Mac and Windows (using Unity) to run FFMPEG to convert a video to a .ogv video. My code is as follows:
string command = "ffmpeg -i '" + filepath + "' -codec:v libtheora -qscale:v 10 -codec:a libvorbis -qscale:a 10 -y '"+workingDir+"/ogv_Video/"+System.IO.Path.GetFileNameWithoutExtension(filepath)+".ogv'";
UnityEngine.Debug.Log("Command: "+command);
try{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo (workingDir+"/..", command);
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.FileName =workingDir+"/ffmpeg";
//Process.Start (startInfo);
Process p = Process.Start(startInfo);
p.EnableRaisingEvents = true;
string strOutput = p.StandardOutput.ReadToEnd();
UnityEngine.Debug.Log ("Running..."+strOutput);
p.WaitForExit();
UnityEngine.Debug.Log ("Got here. "+strOutput);
int exitCode = p.ExitCode;
UnityEngine.Debug.Log ("Process exit code = "+exitCode);
}
catch(Exception e) {
UnityEngine.Debug.Log ("An error occurred");
UnityEngine.Debug.Log ("Error: "+e);
}
The command executes and does not through any exception. However, it terminates instantly and prints Exit Code 1 which is "Catchall for general errors" -this seems not too helpful!
What am I doing wrong with my code, please?
You'll notice that my code prints out the command in full. If I copy that command and paste it into the terminal, it runs absolutely fine.
It turns out I was setting up the arguments wrongly. Referring to this Stack Overflow question, I was able to produce the expected result with the following code:
try{
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +#"ffmpeg";
process.StartInfo.Arguments = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
JSONDataObject rtnMsg = new JSONDataObject("StartConvertOK", "-1", new List<string>());
return JsonUtility.ToJson(rtnMsg);
}
It does seem as though the answer was not that different from what I was doing, but it does work!

How to close a child command window when using Start.Process();

I'm looking to trigger the child command window's close event once its command is finished. Keep in mind, it's a background process initiated from a console app so it's never visible. What is visible is the console application.
I tried using the Exited event, but that didn't work. I tried relying on CMD to know when to close it by using /c, /k, and exit. Neither seem to work. I also tried a do while loop checking HasExited, none of these have worked unless I type "exit" within the application console window. It does not close, but somehow triggers the invisible child command windows to close.
Is there another way of closing it once the child command is complete?
String msg = "echo %time%; exit;";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = msg;
p.EnableRaisingEvents = true;
p.Exited += p_Exited;
p.Start();
msg += p.StandardOutput.ReadToEnd();
Thank you very much!!
I modified your program slightly to run a child command processor, capture its output, then write it to console.
char quote = '"';
string msg = "/C " + quote + "echo %time%" + quote;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = msg;
p.EnableRaisingEvents = true;
p.Exited += (_, __) => Console.WriteLine("Exited!");
p.Start();
string msg1 = p.StandardOutput.ReadToEnd();
Console.WriteLine(msg1);
Here's a full program, using slightly different syntax, but similar in spirit:
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char quote = '"';
var startInfo = new ProcessStartInfo("cmd", "/C " + quote + "echo %time%" + quote)
{ UseShellExecute = false, RedirectStandardOutput = true };
var process = new Process { EnableRaisingEvents = true };
process.StartInfo = startInfo;
process.Exited += (_, __) => Console.WriteLine("Exited!");
process.Start();
string msg1 = process.StandardOutput.ReadToEnd();
Console.WriteLine(msg1);
Console.ReadLine();
}
}
}
Or, as this answer illustrates, maybe just call DateTimeOffset.Now. If you're interested in looking at sub-second info, maybe use Stopwatch class instead.
If you prefer to drive command line with commands from C#, it's also possible. Igor Ostrovsky describes how to convert events to Tasks; then use async/await to create a procedural-looking sequence of commands and responses.

pass multiple arguments to an EXE from Windows form Application

I have an app.exe application that asks to enter input path string, once i enter, it asks output path string... now when i enter, app.exe perform some operation
i need to pass these paths from my Window Form Application
i saw a lot of questions like this but could not implement what i require because i never worked with processes and Stream Reader or Writer
any help please... examples will be thanked.. thank you..
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.WaitForExit(3000);
process.Close();
ok i tried that
but its giving some exception
StandardOut has not been redirected or the process hasn't started yet...
my code was
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Arguments = input + ";" + output;
process.Start();
string Strout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
You can use ProcessStartInfo.Arguments for this.
Process process = new Process()
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
....
process.Arguments = input + " " + output;

run vb script using script.exe

I want to run vbscript file using cscript.exe.
i searched a lot but did'nt found any way while i can run my script using cmd with cscript.exe
this is my code
Process p = new Process();
p.StartInfo.Arguments = #"C:\\Program Files\\VDIWorkLoad\\WorkLoadFile\\open test.vbs";
p.StartInfo.FileName = "testing";
p.StartInfo.UseShellExecute = false;
try
{
p.Start();
p.WaitForExit();
Console.WriteLine("Done.");
}
any idea how i can use cscript.exe
You should set the FileName property to the executable you want to run. In your case that would be cscript.exe and not testing:
p.StartInfo.Arguments = #"""C:\Program Files\VDIWorkLoad\WorkLoadFile\open test.vbs""";
p.StartInfo.FileName = #"C:\Windows\System32\cscript.exe";

I want to delete multiple lines of file's output .which file is redirected

I want to delete multiple string lines of files output.which file is redirected.
My code is as follows.
static void Main(string[] args)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/C ipconfig";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
}
OUTPUT : I give correct answer.but i want only ip address of m/c. Other lines are deleted.
please give the answer of question with changes of code.
Do you really need to run ipconfig? The NetworkInterface class should be able to provide you the information you are looking for without requiring you to run an external process and parse text.
static void Main(string[] args)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/C net view";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
}
OUTPUT :
server name remark
//st1
//st2
//satishlap
//st6
//st10
command is completed successfully.
I give correct output.I want to only server name(//st1,//st2,//satishlap,//st6,//st10).
other infoemation is deleted. please the answer of my question with the changes of code.

Categories

Resources