How to run batch file using IronPython - c#

I have newly started working with IronPython. I have a batch file with some commands.
This file needs to be executed from IronPython. Can it be accomplished?
I was trying to add .net reference but couldn't create object for class Process. Is the approach correct?
import clr
clr.AddReference("System.Diagnostics")
from System.Diagnostics import Process

System.Diagnostics is a namespace.
clr.AddReference accepts System.Reflection.Assembly objects and/or assembly names., e.g.:
clr.AddReference("System.Windows.Forms")
The Process class is in assembly "System.dll" so you don't have to add anything.
Try this:
from System.Diagnostics import Process
p = Process()
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = False
p.StartInfo.FileName = 'C:\\ping.bat'
p.Start()
p.WaitForExit()
print(p.ExitCode)

Related

System cannot find file specified

So we need to execute a exe in our .net API, and this exe is built at the same time as the API itself in our pipelines.
The exe is present in the exact same directory as our .NET API exe and ddl files.
I used the below code to try and trigger the exe:
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "customProgram.exe";
process.StartInfo.Arguments = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
process.Start();
process.WaitForExit();
This throws an error:
An error occurred trying to start process 'customProgram.exe' with working directory '/app'. No such file or directory
Try to specify the full path to the file.
For example like here:
Process.Start(#"C:\Program Files\Google\Chrome\Application\chrome");

Is it Possible to Run a C# project using Another c# project

is it possible to run a C# project Already build using another project? there is any way to invoke the c# execution using a c# Code.
In here I need to execute the Already Compile Project inside a another project.
You can run an exe using Process.Start
ProcessStartInfo ps = new ProcessStartInfo("YourExecutable.exe", "any arguments");
ps.WindowStyle = ProcessWindowStyle.Hidden; // or exclude this line to show it
ps.CreateNoWindow = true; // and this line
ps.UseShellExecute = false;
Process process = new Process();
process.StartInfo = ps;
process.Start();
There are additional ways to get the output of your process for displaying or logging but that is easily searchable.

C# start Child Process (in CMD window) as Administrator with custom environment variables

I have a program which wraps around some Windows SDK executables and opens them in a separate CMD window.
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C signtool.exe [args] & pause";
process.StartInfo.Verb = "runas";
process.Start();
Right now, I have the Windows SDK folder added to my system's Path environment variable. Is there a way to programmatically add the Windows SDK folder to the Path environment variable of the user OR run the process with the SDK folder added to the Path variable of that particular CMD window?
This is the folder I need added to each CMD window's Path variable:
C:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x86
This sub-process must run as administrator. It does not need to receive the output of the child process.
Use a ProcessStartInfo and its Environment property instance to set this up.
var startInfo = new ProcessStartInfo();
var defaultPath = startInfo.Environment["PATH"];
var newPath = "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.16299.0\\x86" + ";" + defaultPath;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c set > D:\\env.txt";
startInfo.Verb = "runas";
startInfo.Environment["PATH"] = newPath;
startInfo.UseShellExecute = false; // required to use Environment variables
Process.Start(startInfo);
There are a number of hurdles to overcome here.
As you've discovered, the Environment and EnvironmentVariables properties of ProcessStartInfo cannot be used when UseShellExecute is true (an exception is thrown). But Verb only works when UseShellExecute is false (the Verb is silently ignored). This comes down to the differences between CreateProcess (the core Win32 function) and ShellExecute/ShellExecuteEx (the shell function).
As another commenter suggested, you might try setting the environment in the parent process and then starting the child process. However, elevated processes don't inherit the environment from a non-elevated parent process.
I would be willing to bet that there is a way to do what you want using a correct series of Win32 calls to get an elevated token and then calling something like CreateProcessAsUser. I am also willing to bet it'll be a little error-prone and ugly in C# because of the necessary struct marshaling. So instead of trying to figure that out for you, I'll offer another suggestion:
Just programmatically write a batch script that sets the environment and invokes signtool.exe. You can then invoke that batch script using the runas verb as you're currently doing.

Guidance for invoking .jar file through asp.net code

ProcessStartInfo psi = new ProcessStartInfo("java.exe", " -jar \"C:\\craFVUsubsreg-lite_APY.jar\"");
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
I want to invoke the utility tool, which is of java.jar file within the "c#" [mvc] application and it is work fine when i run the application through code. However after i published the application and then i try to invoke the same jar file it is not responding or i can say it is not working.
Above is the process code which i'm using to invoke the jar file. Even the user permission i have setted to full control mode.
Kindly guide me, where i am wrong, or is their any way to directly invoke the java.jar file without converting to java.exe.

How to run exe from c# dll which is called from asp

I have create c# dll which is contained in web directory of the product. This dll contain a function which accept some parameters and call a exe (phantomjs.exe) with accepted parameters.I also have write some logging code for file writing in this function. Now I call this dll from asp classic code. Then I found that log maintain by this dll but work of phantomjs.exe does not occur. While when I call this dll from console application then phantomjs.exe works.
I use below code to call the exe
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = pDirectory + "\\res\\bin\\phantomjs.exe";
p.StartInfo.Arguments = arguments;
p.Start();
p.WaitForExit();
Please tell me will there be a need to sign this phantomjs.exe to run in webdirectory or any thing else.

Categories

Resources