Process.Start on gpg.exe just hangs - c#

I'm trying to run gpg.exe from C# using ProcessStartInfo()/Process.Start(). When I run it from cmd it works. I copy and paste the exact command I ran from cmd into my C# app and gpg.exe opens but it just hangs. Doesn't show any text in the window and just sits there doing nothing. What could be going on?
var gpg = new ProcessStartInfo(#"C:\GnuPG\gpg.exe", #"--batch --yes --output c:\applications\development\invoicedecryption\temp\invoice.csv --passphrase-fd <C:\GNUPG\pasfraz.txt --decrypt C:\applications\development\invoicedecryption\temp\Invoices.csv.pgp");
var prs = new Process();
prs.StartInfo = gpg;
prs.Start();

Related

aerender from C# console Application

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

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;");
}
}

c# open cmd.exe process and Execute multiple commands

I would like to be able to open cmd and execute two commands from the window. First I would like to navigate to a particular directory where I can then run the second command from. Running a single command is pretty easy as this is all I have to do:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + #"\Cisco Systems\VPN Client\";
Process process = new Process();
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", #"/c cd " + path );
process.StartInfo = processInfo;
process.Start();
However am not sure of the way to add the second argument so it runs after cmd runs the first command. Some research led me to this code snippet. Am unsure if this works since my aim is to start cisco vpn client from cmd and this seems not to start it. Here is the code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + #"\Cisco Systems\VPN Client\";
Process process = new Process();
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", #"/c cd " + path + "-t vpnclient connect user validuser pwd validpassword nocertpwd validconnectionentry ");
process.StartInfo = processInfo;
process.Start();
I once started the vpn client from cmd with the credentials just to make sure they were valid and it worked but I cant pull it off via C# programmatically.
Regards.
There three things you can do to achieve what you want. The easiest is to set the working directory of the process through ProcessStartInfo. This way you will only have to execute the command to start the VPN client.
The second option is redirecting the input and output of the process. (Also done through the ProcessStartInfo) This is something you want to do if you need to send more input to the process, or when you want to retrieve the output of the process you just started.
The third option is to combine the two commands with the & symbol. Using the & symbol makes cmd.exe execute the two commands sequentially (See here for an overview of the available symbols). Using this option will result in a command like this: /c cd path & vpnclient.
However because you just want to change the working directory of the process using the first option makes your code more readable. Because people reading your code do not need to know the & symbol in bash to understand what your code does. Changing the working directoy is done with the WorkingDirectory (MSDN) property of ProcessStartInfo (MSDN). See the following code:
var processInfo = new ProcessStartInfo("cmd.exe", #"/c vpnclient connect user validuser pwd validpassword nocertpwd validconnectionentry ");
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = path;
You can use & to execute next command or && to execute following command only if the previous one succeeded.
Examples:
dir /b & cls
and
taskkill /f /im explorer.exe && start explorer

Categories