Keeping Escape Characters in C# - c#

I may be completely doing this wrong, or overlooking something obvious but here it goes. I am working with AWS CLI via C#. I am trying to send a command to the command line to change the instance type of the EC2
I've tried different ways of handling this, starting the string with #, single quotes encapsulating double quotes, etc. Here is what I currently have:
private static void resizeCurrentInstance(string instance)
{
string commands = "/C aws ec2 describe-instances --instance-ids " + instance;
string response = getNewProcess(commands);
JObject convertedResponse = JObject.Parse(response);
int i = 0;
string ReturnString = "";
foreach (JObject item in convertedResponse["Reservations"])
{
if (item["Instances"][i]["InstanceType"].Value<string>().Contains(".large"))
{
string sizeUpCommand = "/C aws ec2 modify-instance-attribute --instance-id" + instance + " --instance-type \"{\"Value\": \"m4.4xlarge\"}\"";
string sizeUpResponse = getNewProcess(sizeUpCommand);
Console.WriteLine("Instance Size inscrease " + sizeUpResponse.ToString());
}
else
{
string sizeDownCommand = "/C aws ec2 modify-instance-attribute --instance-id " + instance + ' --instance-type \"{\"Value\": \"m4.large\"}\"';
string sizeDownResponse = getNewProcess(sizeDownCommand);
Console.WriteLine("Instance Size decrease " + sizeDownResponse.ToString());
}
}
}
The problem comes from trying to generate the command string to send to the command line here
string sizeUpCommand = "/C aws ec2 modify-instance-attribute --instance-id" + instance + " --instance-type \"{\"Value\": \"m4.4xlarge\"}\""
This is the command as its give in the amazon docs:
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --instance-type "{\"Value\": \"m1.small\"}"
I'm trying to figure out how to write a string so the Output is: "{\"Value\": \"m1.small\"}"

If you need to keep a backslash in the string, then you use \\ before the \".
But you are also missing a space after --instance-id, which would mess up the command.
So it should look something like this:
string sizeUpCommand = "/C aws ec2 modify-instance-attribute --instance-id " + instance + " --instance-type \"{\\\"Value\\\": \\\"m4.4xlarge\\\"}\""
If that doesn't work, then output sizeUpCommand to the console and inspect it to make sure it is what it should be.

Try this:
"{\\\"Value\\\": \\\"m1.small\\\"}"

Related

Postman - how to pass data from List<string>, in memory, into -data portion of Newman command line

I need to execute Postman collection from within my C# method.
But instead of the data file, I need to pass the data directly from the method output (as List).
Here is my code:
public StringBuilder RunPostmanCall(string collectionPath, string executionFolder, string environmentPath, List<string> inputFilePath = null)
{
StringBuilder runOutputBuilder = new StringBuilder();
string runOutput = null;
ProcessStartInfo psiNpm = new ProcessStartInfo
{
FileName = "cmd",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
};
Process pNpmRun = Process.Start(psiNpm);
pNpmRun.OutputDataReceived += (sender, a) => runOutputBuilder.AppendLine(a.Data);
Console.WriteLine(" - Install Newman ...");
pNpmRun.StandardInput.WriteLine($"npm install -g newman");
Console.WriteLine(" - Execute Postman Script ...");
string value = $"newman run " +
$"\"" + collectionPath + "\" " +
$"--folder \"" + executionFolder + "\" " +
$"--environment \"" + environmentPath + "\" " +
$"-d \"" + inputFilePath + "\" " +
$"--disable-unicode";
pNpmRun.StandardInput.WriteLine(value);
pNpmRun.BeginOutputReadLine();
pNpmRun.StandardInput.WriteLine("exit 0");
I'm getting the following error:
bin\Debug>newman run "../../api/postman_audit.json" --folder "SearchIndex" --environment "../../api/postman_environment.json" -d "System.Collections.Generic.List`1[System.String]" --disable-unicode
I could save the output into the file, and then just use that file location in the command-line. But I would like to avoid creating a file, and read data directly from the memory.
unfortunately with -d you can only refer to a file in your filesystem.
If you are not willing to write this data directly into a file, i would suggest to set the values directly as a global variable from commandline.
Try to add this parameter to your newman run command
--global-var key=value
You can add your data into a string and add it as a global variable. You can parse it in your pre-request or test-script normally.

How do I replace PATH Environment variables to double quote + backslashes those which have spaces in C#?

Language version: C# - Operating system: Win 10 - IDE: Visual Studio
I've got a problem when letting the user set a path.
When the path is without spaces, all good. No issues.
When the path is with spaces, the CLI i'm forwarding the data to, doesn't accept it and instantly closes.
That's because it misses the right syntax (double quotes at beginning & end + double backslashes) when it arrives.
So the right syntax is:
app.exe -q MP3_128 -p "C:\\test test\\"
Since I'm using the Environment.GetFolderPath in WMF, I have no clue how to add these to its output...
How do I replace PATH Environment variables to double quote + backslashes those which have spaces in C#?
Code:
if (Settings.Default.download == "")
{
MessageBox.Show("Be sure your download path doesnt contain any spaces!");
String path = Environment.GetFolderPath (Environment.SpecialFolder.MyMusic);
String pathDouble = path.Replace("'", "\"");
Settings.Default.download = #"""C:\\Test\DOWN LOADS""";
Settings.Default.Save();
}
which been reached here in
Code:
if (Settings.Default.sm != "")
{
download.StartInfo.FileName = Settings.Default.sm;
string a = " -q " + qualitys + " -p " + Settings.Default.download + " " +
info[result.SelectedIndex].link;
Debug.Write(a);
download.StartInfo.Arguments = a;
}
The hardcoded default path set works.
But when the user changes that path using the GUI to their own likings, it's gone.
I think I'm editing the wrong code.
This code, I have edited, which only now needs a double \ after the drive:
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == DialogResult.OK)
{
Settings.Default.download = #"""" + folderDialog.SelectedPath + #"\\""";
Settings.Default.Save();
txb_download_folder.Text = Settings.Default.download;
}
}
But how?
A simple \ extra in the first #"\" results in \"C:\test test\\".

ProcessBuilder for .NET Core

In Java I have code:
ProcessBuilder builder = new ProcessBuilder(fileName, license, session, server, db, user, pass, port);
Process process = builder.start();
And in .NET Core I tried this:
String builder = license + " " + session + " " + server + " " + db + " " + user + " " + pass + " " + port;
Precess process = new Process();
process.Start(fileName, builder);
And the version with ProcessStartInfo:
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = builder;
but it does not work. I have error:
Member 'Process.Start(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead
How in .NET Core should I do it?
Use
var process = Process.Start(fileName, builder);
As the error message tells you, Start is a static method and you cannot call static methods on class instances (you can in Java, but that's still a warning).
Also note that you may have to manually quote arguments containing spaces, which Java would have done for you, cf. the example here.

Java (Minecraft) doesnt start from c#

When I copy paste the javaw.exe -arguments to console it works but, when I launch it like this it doesn't work.
string directory = "C:\\Users\\Can\\AppData\\Roaming\\.minecraft";
string java = #"C:\windows\system32\javaw.exe";
string javaLocation = "C:\\Program Files\\Java\\jre7\\bin\\javaw.exe";
string RAM = "1G";
string username = "namehere";
string token = "--session token:"+tokenGenerated;
string version = "1.6.2";
string launch = "-Xmx" + RAM + " -Djava.library.path={0}\\versions\\1.6.2\\1.6.2-natives-7453523379463 -cp {0}\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.5\\jopt-simple-4.5.jar;{0}\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;{0}\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;{0}\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;{0}\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;{0}\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;{0}\\libraries\\argo\\argo\\2.25_fixed\\argo-2.25_fixed.jar;{0}\\libraries\\org\\bouncycastle\\bcprov-jdk15on\\1.47\\bcprov-jdk15on-1.47.jar;{0}\\libraries\\com\\google\\guava\\guava\\14.0\\guava-14.0.jar;{0}\\libraries\\org\\apache\\commons\\commons-lang3\\3.1\\commons-lang3-3.1.jar;{0}\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;{0}\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;{0}\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;{0}\\libraries\\com\\google\\code\\gson\\gson\\2.2.2\\gson-2.2.2.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.0\\lwjgl-2.9.0.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.0\\lwjgl_util-2.9.0.jar;{0}\\versions\\1.6.2\\1.6.2.jar net.minecraft.client.main.Main --username " + username + " " + token + " --version " + version + " --gameDir {0} --assetsDir {0}\\assets";
launch = String.Format(launch, directory);
string text = launch;
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
Directory.SetCurrentDirectory(#"C:\windows\system32\");
Process.Start("javaw.exe",
Path.Combine(launch));
What am I doing wrong?
Why do you need to call Path.Combine if your whole path is in one string?
Assuming your javaw.exe is actually in C:\windows\system32\, Process.Start("java.exe", launch); should work as intended.
Source - Path on MSDN: http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Just use Java's real location instead of "javaw" like "c:\programfiles\Java\jre7\bin\javaw.exe"

C# command-line argument with newline fails in C# but not in python. Need to make the c# work

Env: .NET 3.5 Visual Studio 2008 SP1, on Win XP SP3, Python 2.7. Corporate image, no admin rights.
In C# 3.5, I want to pass a parameter to a command-line program, and this parameter contains newlines. It works in Python 2.7 but not in C#.
When body contains newline, c# truncates the result, but python passes it correctly.
Python code
cmd = self.app_path + ' email -Subject "' + subject + '" -From "' + address_from + '" -To "' + address_to +'" -Body "' + body +'"'
cmd_result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
C# code:
string Command = "<path to executable>";
string arguments = " email -From " + FromAddress + " -To " + ToAddress + " -Subject \"" + SubjectLine + "\" -Body \"" + emailBody + "\" ";
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
start.CreateNoWindow = false;
start.UseShellExecute = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);
Any idea?
Update: body in the python example and emailBody in the c# example contain the same string, as strings are represented in each language.
Update: noticed the command wasn't terminated correctly in the python code. I added + '"' at the end of line 2. The code ran as before.
Also, as you can tell, the app called sends the body (emailBody) parameter content as an email body.
Sorry but I think it is the process you call that does not take the arguments correctly; it is not the C# that is involved, and as a proof of that:
Create a new windows form application
Make sure that the Main signature looks like this:
static void Main(string[] args)
Make sure that the call of your Main Form (named Form1) looks like this:
if (args==null)
{
Application.Run(new Form1());
}
else
{
Application.Run(new Form1(args));
}
Add a textbox to your form, with the multiline property set to true
Add a button to your form
Run the application once
Then Put this in the code of your button1_Click function (replace TheNameOfTheCurrentApp):
string Command = "TheNameOfTheCurrentApp";
string arguments = textBox1.Text;
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
start.CreateNoWindow = true;
start.UseShellExecute = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);
Add this constructor to your form:
public Form1(string[] args)
{
InitializeComponent();
textBox1.Text = string.Join(" ", args);
}
Then run your app, insert muliple lines in your textbox and click the button. The same app will appear with the textbox correctly filled with the line breaks

Categories

Resources