aerender from C# console Application - c#

I'm trying to render an Adobe After Effects composition from a C# Console Application but it's not working. It doesn't even give any error, the code executes smoothly.
string strCmdText = #"""C:\\Program Files\\Adobe\\Adobe After Effects 2022\\Support Files\\aerender -project C:\\ForRadio\\AE_Comp\\InputScenes.aep""";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Process cmd = new Process();
string strCmdText = #"""C:\\Program Files\\Adobe\\Adobe After Effects 2022\\Support Files\\aerender -project C:\\ForRadio\\AE_Comp\\InputScenes.aep""";
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = strCmdText;
cmd.Start();
I have tried both the variations of code and it's not throwing any error. When I run this command in the command prompt it works, but not when I execute from within my C# console app.
C:\\Program Files\\Adobe\\Adobe After Effects 2022\\Support Files\\aerender -project C:\\ForRadio\\AE_Comp\\InputScenes.aep

Related

How to execute 2 different commands cmd on C#

I was working on a project which needs to run two cmd commands on C#, I looked up on how to do it but nothing worked for me, and everyone was giving the solution to execute only one cmd command.
I would like to execute one command when the user clicks a button, then another one right after, without quitting the cmd, and if possible to hide the cmd window.
My goal would be to execute the 2 following commands in the cmd, to run a Run.bat file:
cd C:\users\user\documents\file
Run.bat
Thanks.
You can directly call "C:\users\user\documents\file\Run.bat" and set the working directory as well as the shell execute flag :
string path = #"C:\users\user\documents\file\";
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = path + "Run.bat";
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
process.Start();
//process.WaitForExit();

Trigger a PS1 file from C# code. C# code has to be wrapped in exe

I have a powershell script which I want to trigger from an EXE.
Things already tried or cannot do:
Can't run powershell directly because process running it is expecting an exe and not a powershell.
Powershell script is pretty complex, so porting it to C# will not be trivial and will be very time consuming.
PS2EXE works, but cannot use because of security policy.
Tried to google on how to create an exe for a ps1 file, but couldn't find any solution (except ps2exe). So I decide to try to execute ps1 using C# and then create an exe. Then wrap both exe and powershell in same msi.
Tried by simply creating Process and starting it:
Process p = new Process();
p.StartInfo = new ProcessStartInfo("powershell.exe");
p.StartInfo.Arguments = #"-Executionpolicy unrestricted C:\script\ms.ps1";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
Above code is working, however it is flashing a black cmd windows for a millisecond or so.
Tried following code from source: https://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C
string scriptText = System.IO.File.ReadAllText(#"C:\script\ms.ps1");
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
pipeline.Invoke();
runspace.Close();
Same problem as above, flashing a black windows for few seconds.
Answer to any of following questions can solve my problem?
How can I create an exe for powershell without using PS2EXE?
How can I disable windows popup when invoking powershell from C# code?
Is there any other way I can solve my problem?
--
Thanks
Hello! You can select "Windows Application" in the project properties instead
of the console
Maybe it help you for this: /flashing a black cmd windows/
Also you can run commands whithout running the script (something like
this):
string MyCommand = "-Command &{ if (!(Test-Path 'c:\\test')) {md 'c:\\test'; get-process | Out-File c:\\test\\MyFile.txt}}";
ProcessStartInfo MyProcInfo = new ProcessStartInfo();
MyProcInfo.FileName = "powershell.exe";
MyProcInfo.Arguments = MyCommand;
Process MyProcess = new Process();
MyProcess.StartInfo = MyProcInfo;
MyProcess.Start();
MyProcess.WaitForExit();

write command to command prompt open via executing bash file

I am working on developing the visual studio plugin. As a part of it, I want to execute one bash file. This bash file opens the command prompt.
Once command prompt is open , we want to write / executes bunch of commands on it.
I have tried it like this:
System.Diagnostics.Process process = System.Diagnostics.Process.Start("MyBash.bat");
process.WaitForExit(5000);
process.StandardInput.WriteLine("echo %PATH%");
But I can see that, it open the command prompt but fail to write command on it.
It throws the exception at the line of writing the command to it. It seems like the command prompt open from this bash file has different process id.
Please help me to resolve it.
Try to execute directly in your batch file:
Insert this to your batch:
start cmd.exe /k "echo %PATH%"
You can also use
/c
if you want to close the cmd After Execution
EDIT:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.FileName = "mybash.bat";
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}

How to close a cmd window after I ran an exe. file from it?

I have a c# app (app.exe), which I want to run from a command line window and then to CLOSE the command line window after the app started.
I tried to search for "cmd" in the processes list and to close it (cmdProcess.CloseMainWindow()) but in this case, if I run app.exe by double-click only, and there is another cmd opened separately, it will be closed- and I don't want that.
How can I know which cmd instance runs my app?
thanks
In Windows Command prompt it's "&" to attach commands in one line. So it'll be:
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/k " + Command + " & exit";
But if you read the "cmd /?", you'll see that the purpose of "/k" argument is to keep the window. So if it's not what you want, just use the "/c" argument instead.
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + Command;
try this
Process process = new Process();
process.StartInfo.Arguments = Command + "; exit";
the ";" is to separate between the commands and the "exit" is the next command to execute

Using cmd.exe from C#

I have been toying with the Process class in C#. I have some code below I'd like to use to open cmd.exe and run the DIR command. However, when I attempt to use the code, the cmd.exe opens, but no command is run. Why is this happening and how do I fix it?
Process cmd = new Process();
cmd.StartInfo.FileName = #"cmd.exe";
cmd.StartInfo.Arguments = #"DIR";
cmd.Start();
cmd.WaitForExit();
Try passing the /K option to let the command console stay at video and receive the subsequent DIR command (Without exit).
Process cmd = new Process();
cmd.StartInfo.FileName = #"cmd.exe";
cmd.StartInfo.Arguments = #"/K DIR"; // <-- This will execute the command and wait to close
cmd.Start();
cmd.WaitForExit();
The /K option will allow you to get a better understanding of what happens in the command window because the window will not close immediately and you need to click the close button or type the Exit command. If you want to exit after issuing your commands then use the /C option.
cmd.StartInfo.Arguments = #"/c DIR";

Categories