Getting the command prompt c# - c#

The following code executes a .exe file on the local machine and generates a new file .obj based on an .ifc file in the same directory. .obj (new one) and .ifc should have the same name (no problem).
if (clicked) {
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = "C:\...";
string arg1;
arg1 = "#/c command" // run .exe appliaction
p.StartInfo.Arguments = arg1;
p.Start();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.WaitForExit();
}
If the user clicked the button one more time (the code gets run again), a new cmd appears and tells him that the file already exists. Does he want to override:
(y/n) would be the argument in this case.
Now I want to capture that new cmd and replace it with a "yes/no" window, so that the user wont see it. How is this possible?

Related

Start NodeJS Application from C# web Form Application

I have built MeteroJS application that I want to start as NodeJS application from C# code.
Here is Windows Form application that is used as control panel for starting and stopping the NodeJS application
I can start NodeJS application manually with command line: (this works!)
set MONGO_URL=mongodb://someAdmin:password#localhost:27017/some_db
set ROOT_URL=http://someservice:8080
set PORT=8080
node bundle\main.js
I want to repeat all action above from command line, this time inside C# application.
This is code that executes on Start button click:
Environment.SetEnvironmentVariable("MONGO_URL", String.Format("mongodb://{0}:{1}#localhost:27017/{2}", usernameTxt.Text, passwordTxt.Text, databaseTxt.Text), EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("ROOT_URL", String.Format("http://someservice:{0}", portTxt.Text), EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PORT", portTxt.Text, EnvironmentVariableTarget.Machine);
Process.Start("CMD.exe", #"/C node bundle\main.js");
I am not sure if this is even possible. This simply does not work and left no logs.
Could you please check what I am doing wrong and advise.
Use the following code to execute the node.js cmd
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"c:\node\node.exe";**//Path to node installed folder****
string argument = "\\ bundle\main.js";
p.StartInfo.Arguments = #argument;
p.Start();
This code may help you:
Process p = new Process();
p.StartInfo.WorkingDirectory= #"C:\Users\USERNAME\Documents\Visual Studio 2015\Projects\Proyecto 1.3\Proyecto 1.3\bin\Debug\server";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c node app.js";
p.Start();

Run an exe with command-line parameters

I have an exe that I would like to use by executing it with command line parameters.
var query = Path.Combine(path, calculator.ExeName + ".exe");
var p = new Process();
p.StartInfo.FileName = query;
//the command line parameter that causes the exe to start in an invisible mode
p.StartInfo.Arguments = "episrc"
p.Start();
This code works and it starts the exe but there's one problem : the exe is supposed to be writing on a file in its directory but that doesn't happen. The process exits successfully (Exitcode0). What could be the cause of this problem?
I have a Delphi code that executes successfully the exe and the exe writes to the file but it's using the ExecProcess from win32 API thus the exe is valid and working.
Also if I try to execute it from the command prompt like so : kowwinnt.exe episrc it writes to the file successfully.
You should set the Working Directory.
Your code would look like this:
var query = Path.Combine(path, calculator.ExeName + ".exe");
var p = new Process();
p.StartInfo.FileName = query;
p.StartInfo.WorkingDirectory = path;
//the command line parameter that causes the exe to start in an invisible mode
p.StartInfo.Arguments = "episrc"
p.Start();

How to redirect command prompt output to a file using asp.net C#?

I have tried to redirect the command prompt output to a file using Asp.Net C#.
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "c:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = #"/c dir" +">" + #"Myval.txt";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
Response.Write(output);
si.Close();
The file is getting created successfully but no content present in it.
Even the variable Output returns nothing.
Help me to resolve this issue.
EDIT after being corrected:
I just tested on my machine and the code works perfectly. I apologize for not reading and testing carefully myself. Myval.txt is created and the DIR output is written into it.
The output variable is empty because you are rerouting any output by the DIR command into the txt file, so that's by design.
Please see if there are any locks on the txt file preventing it from being overwritten. Further than that, I can only guess that there is a security issue preventing the DIR command from running.
IIS7 - I tested this various ways including using a Batch file but the application isn't available on desktop. I can see the worker process and the exe running under my user name but with session id value of zero.
The following has worked for me through command prompt:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Passing arguments one by one one in console exe by c# code

I want to run an exe file by my c# code. The exe file is a console application written in c#.
The console application performs some actions which includes writing content in database and writing some files to directory.
The console application (exe file) expects some inputs from user.
Like it first asks , 'Do you want to reset database ?' y for yes and n for no.
again if user makes a choice then application again asks , 'do you want to reset files ?'
y for yes and n for no.
If user makes some choice the console application starts to get executed.
Now I want to run this exe console application by my c# code. I am trying like this
string strExePath = "exe path";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = strExePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
I want to know how can I provide user inputs to the console application by my c# code?
Please help me out in this. Thanks in advance.
You can redirect input and output streams from your exe file.
See redirectstandardoutput
and redirectstandardinput for examples.
For reading:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
For writing:
...
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("y");
...
myStreamWriter.Close();
ProcessStartInfo has a constructor that you can pass arguments to:
public ProcessStartInfo(string fileName, string arguments);
Alternatively, you can set it on it's property:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";
Here is a sample of how to pass arguments to the *.exe file:
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = #"\filepath.exe";
p.StartInfo.Arguments = "{insert arguments here}";
p.Start();
error += (p.StandardError.ReadToEnd());
p.WaitForExit();

Is this possible to run a python code from C# through command prompt?

I want to run python code from C# through command Prompt.The Code is attached below
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"d:";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"cd D:\python-source\mypgms");
p.StandardInput.WriteLine(#"main.py -i example-8.xml -o output-8.xml");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
Output :
D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml
D:\python-source\mypgms>
But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.
But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue...
Thanks & Regards,
P.SARAVANAN
I think you are mistaken in executing cmd.exe. I'd say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.
At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:
p.StandardInput.WriteLine(#"exit");
But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.
I think you need something along these lines:
var p = new Process();
p.StartInfo.FileName = #"Python.exe";
p.StartInfo.Arguments = "main.py input.xml output.xml";
p.StartInfo.WorkingDirectory = #"D:\python-source \mypgms";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.
Why not host IronPython in your app and then execute the script?
http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx
http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application
or use py2exe to pragmatically convert your python script to exe program.
detail steps...
download and install py2exe.
put your main.py input.xml and output.xml in c:\temp\
create setup.py and put it in folder above too
setup.py should contain...
from distutils.core import setup
import py2exe
setup(console=['main.py'])
your c# code then can be...
var proc = new Process();
proc.StartInfo.FileName = #"Python.exe";
proc.StartInfo.Arguments = #"setup.py py2exe";
proc.StartInfo.WorkingDirectory = #"C:\temp\";
proc.Start();
proc.WaitForExit();
proc.StartInfo.FileName = #"C:\temp\dist\main.exe";
proc.StartInfo.Arguments = "input.xml output.xml";
proc.Start();
proc.WaitForExit();

Categories

Resources