C# Process.Start Argument with a dash fails to executr - c#

I am having some trouble trying to work my way through some C# that I'm just not comfortable with. My issue appears to stem from a dash/hyphen in one of my parameters being passed to Process.Start(). In the below code the argument list is built from data in the database. The issue occurs when sRecipient has a dash. For example Bank-Name vs BankName. Unfortunately, I can't remove the dash because it is the name of a public key provided by the bank.
sGPGParms = " -e ";
if (sUseGPGASCIIArmor.ToUpper() == "Y")
{
sGPGParms += "-a ";
}
sGPGParms += "-r \"" + sRecipient + "\" \"" + sDestinationDir + sFileName + "\"";
The next section of code is where things seem to fail. The program is using Process.Start() to pass the path and the parameters. The program does not fall into the fail condition and says that the file created successfully but it doesn't do anything. This code worked earlier today before the bank provided a new encryption key with a dash in the name. That dash is literally the only change. This code has worked since 2008 so I have isolated it to the arguments parameter in Process.Start() and how the dash is impacting things as the culprit.
Process myCmd;
if ((myCmd = Process.Start(Path, sGPGParms)) == null)
{
LogEvent(sServerLogPath, "ERROR: GPG.exe failed to start", sUserName);
SqlContext.Pipe.Send("GPG.exe failed to start");
}
else
{
LogEvent(sServerLogPath, "Bank file created successfully", sUserName);
SqlContext.Pipe.Send("Bann file created successfully");
}
As a side note, I can run the full command copied from the logging and it works. It only fails when passed through Process.Start().
This is the command that works manually but fails when passed through Process.Start() with no public key
"C:\Program Files (x86)\GNU\GnuPG\gpg2.exe" -e -r "Bank-Name" "c:\public\BankPay.txt"
Any ideas why this dash is causing an issue?
UPDATE: Working through some of the suggestions in the comments I see that I am getting an exit code of 2 with the error public key not found. The key exists because I can run the command manually and can see it when I --list-keys

Related

Cannot call cmd.exe from any application when it is called via the MIM .dll

What I currently have is a MIM .dll extension for the metaverse that needs to call the command line to run some console commands. These console commands are for a web tool call GAM that will create G-Suite users. I have a test console app built-in .NET Framework 4.7.2 that works just fine. When running the code with my .dll it seems to process just fine, even returns a positive exit code of 0, but does not perform the function in cmd that is expected. The .dll is in .NET Framework 4.5. The code block is the same for both applications:
using (Process gamProcess = new Process())
{
gamProcess.StartInfo.UseShellExecute = false;
gamProcess.StartInfo.RedirectStandardOutput = true;
gamProcess.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
gamProcess.StartInfo.CreateNoWindow = true;
gamProcess.StartInfo.Arguments = "/c gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
gamProcess.Start();
gamProcess.WaitForExit();
}
If I call this block of code directly from the .dll, it will act as if it performs the action but nothing happens. If I call this code from the standalone application, it works just fine. I have even attempted to just call this application from the .dll and pass arguments directly to it. This will work if I set the arguments in Visual Studio but it will not work if I call the application from the .dll. I'm at a loss here, I cannot figure out what is the difference between running from my extension and running from the application.
Another side note, I built an even more simplistic console application that just writes to a file. I was able to call this application from the .dll, and it works just fine. It is only the commands above that the .dll seems to not want to process correctly.
I managed to fix the above code. I did two things at the same time, so I'm not sure which was the key to this issue but it is working flawlessly now. I changed the /c in the code to a /C uppercase. The code now looks as follows:
using (Process gamProcess = new Process())
{
gamProcess.StartInfo.UseShellExecute = false;
gamProcess.StartInfo.RedirectStandardOutput = true;
gamProcess.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
gamProcess.StartInfo.CreateNoWindow = true;
gamProcess.StartInfo.Arguments = "/C gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
gamProcess.Start();
gamProcess.WaitForExit();
}
The second action I performed was a restart of the dev server I was working on. I'm not sure which worked, especially since I was running a lowercase /c in my console app, but the code is working now. Thank you all who viewed this question.

c# Provide packet source in pcap.net

I am following Pcap.net tutorials from its wiki on github. I tried to run code from here:
https://github.com/PcapDotNet/Pcap.Net/wiki/Pcap.Net-Tutorial-Handling-offline-dump-files
I didn't understand the following part:
if (args.Length != 1)
{
Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
return;
}
But I run this code and Bingo, nothings happen (no output).
I tried to figure out and found that args has the value "0".
I comment return command and it start working fine till I got IndexOutOfBound exception here:
using (PacketDumpFile dumpFile = communicator.OpenDump(args[0]))
Did I missed any thing?
This program require 1 argument (i.e. 'filename'in this case) that you should pass through command line.
Compile the code and run the program through command line with argument value.
For Example:
In CLI >MyProg.exe fileNmae

Build visual studio solution using msbuild from c# code

I want to build my solution file from other c# code using msbuid I have tried
var msbuild_path = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
var solution_path = #"D:\Sumit\WorkingCopy\Final\Final.sln";
Process.Start(msbuild_path + " " + solution_path);
but this one throws an error Please help me out!!
According to https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx , the Process.Start method takes two arguments:
public static Process Start(string fileName, string arguments)
So you should change your code to
Process.Start(msbuild_path, solution_path);
What you were doing before was actually trying to run a file named "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe(space)D:\Sumit\WorkingCopy\Final\Final.sln", but no such file exists with that name. The msbuild.exe may exist, but "msbuild.exe D:\Sumit...\Final.sln" is not the filename you meant to pass as the command filename. Also, the argument string was empty, so the system assumed you did not want to pass any arguments to "msbuild.exe D:\Sumit...\Final.sln". But the error message was because the two filenames were mashed into one filename.
Windows allows filenames to contain embedded spaces, which frequently causes problems in dealing with command-line arguments.

How to run command line code from within C# Windows Form?

I am fairly new to coding, but have built a few small things. One thing I figured out on my last project was how to run 2 simple commands normally run from a console, but from within a form application instead. Simply, the form had 2 buttons and clicking one caused ipconfig to run and the other ipconfig /all. It then posted the ip information coming from the command into another form I created as a message box. That is important because I am trying to do something similar and nothing is working now.
I have a form that has a spot for user name and a spot for password. On submit, I want it to essentially run the following:
NET USE F: \\ALPHA\CLIENTAPPS /user:domain\%username% %password% /persistent:no
NET USE O: \\ALPHA\USERS /user:domain\%username% %password% /persistent:no
NET USE S: \\ALPHA\COMPANY /user:domain\%username% %password% /persistent:no
Where %username% and %password% are captured from the form and domain will be our actual domain.
Using similar methods to the aforementioned ipconfig program that is working, this is what I came up with. However, when I click the Submit button, nothing happens, no errors, nor does it actually create the network share:
private void btnSubmit_Click(object sender, EventArgs e)
{
string un = txtUsername.Text;
string pw = txtPassword.Text;
System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", #" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
PR.RedirectStandardOutput = true;
PR.UseShellExecute = false;
PR.CreateNoWindow = true;
System.Diagnostics.Process StartPR = new System.Diagnostics.Process();
StartPR.StartInfo = PR;
StartPR.Start();
}
What am I missing here, or is there a better way? Thanks.
Mike
System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", #" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
Try to remove "#" or remove escaping of "\" char
Info here (Verbatim string literals)
nothing happens, no errors, nor does it actually create the network share
You've done a lot to ensure that. "No errors" is easy to explain, you don't check for errors nor do you give a way for the user to see them because you made sure that the console window isn't visible. If the command failed that it won't be visible. Checking Process.ExitCode is a minimal requirement.
Next flaw is that you create the mapping to the share for a particular user. Which is fine, drive mappings are a per-user setting. But you are not actually logged-in as that user so you can't see those mappings. You'll have to hit Ctrl+Alt+Del and switch the user account. But that's a lost cause because you passed /persistent:no. That means "persistent while the user is logged in".
Ultimate flaw is that you leave it up to an another process to take care of it. That always loses critical information, especially errors. You should pinvoke the Windows api function that does this so you know when it doesn't work and don't burn a gazillion cycles to run another process. Pinvoke WNetAddConnection2().

Running an exe from C# with arguments that have spaces in

I am scratching my head with this one. I am trying to run an exe from C# using system.diagnostics but it isnt passing over my arguments correctly so the exe falls over.
It splits the path after the word 'here' (see below) because of the space in it.
Does anyone know how I can get round this without renaming the directory (which isn't an option for me)
This works from command line:
"C:\Users\me\Desktop\myexternalexe\myexternalexe.exe" comments “\192.168.1.1\a\here is the problem\c\d\"
This doesn't from with in Visual Studio:
Process myexternalexe = new Process();
myexternalexe.StartInfo.FileName = #"C:\Users\me\Desktop\myexternalexe\myexternalexe.exe";
myexternalexe.StartInfo.Arguments = #"comments \\192.168.1.1\a\here is the problem\c\d\";
myexternalexe.Start();
But you've omitted the quotes from the C# version. It should be:
myexternalexe.StartInfo.Arguments = #"comments ""\\192.168.1.1\a\here is the problem\c\d\""";
Did you checked
this
In your case following should work.
string folderName = #"\\192.168.1.1\a\here is the problem\c\d\";
myexternalexe.StartInfo.Arguments= #"comments" + " \"" + folderName +"\"";
Have you tried:
alexe.StartInfo.Arguments = "comments \"\\\\192.168.1.1\\a\\here is the problem\\c\\d\\\"";

Categories

Resources