How to execute a Java app with arguments in c# - c#

I have researched and attempted to do this myself, but with no luck. I am trying to execute a java app and pass it a few variables. It is finding the java and running it, but not passing it any variables, here is what I have so far -
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = #"-jar javaapp.jar" + "host:port" + "password" + "/say" + "test";
startInfo.FileName = "java";
proc.StartInfo = startInfo;
proc.Start();
I cant work this out at all. I dont want to use any third party software (unless it can be packaged in the .EXE) as this is meant to be distributed to people who need it. Any help is thanked.

You need to put spaces in the strings you're concatenating, your command currently looks like
java -jar javaapp.jarhost:portpassword/saytest
The simplest way is to use string.Format:
string command1 = "/say"
string command2 = "test"
string args = string.Format("-jar javaapp.jar {0}:{1} {2} {3} {4}", host, port, password, command1, command2);
startInfo.Arguments = args;

Arguments is a string - the way you have it they'll all be mashed together with no space separating them.
The # symbol in C# is used for literal format of string; it looks like you're trying to use it to build an array or something.

Related

Create new PowerShell process with pipes

Running this from CMD produces the correct result:
powershell -command "& Get-DiskImage -imagepath C:\\file.vhdx | Get-Disk"
<Here is some stuff regarding VHD>
I want to achieve exactly the same running this from C# (there's no way to run it directly, use some PowerShell related .NET stuff, or something else).
My code is the following:
static void LaunchCommandLineApp()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "powershell";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = "-command \" & get-diskimage -imagepath C:\\file.vhdx | Get-Disk \"";
using (Process exeProcess = Process.Start(startInfo)) {
exeProcess.WaitForExit();
var out = exeProcess.StandardOutput.ReadToEnd();
}
}
And in "out" I am getting an error:
Get-Disk : Cannot validate argument on parameter 'Number'. The argument is null. Provide a valid value for the argument, and then try running the command again.
But exactly the same code works in CMD. If I remove "| Get-Disk" from arguments, I will get correct output in "out" from the Get-DiskImage cmdlet.
Also, I have tried to play with curly braces, as other answers suggested - error haven't changed.
What shall I put in "startInfo.Arguments", so my output of "Get-DiskImage" will be correctly piped to the next cmdlet?
This is not actually a problem with the difference between running from the command line and from C#. I created a test VHDX and got the same (error) result whether run from C# or the command line, as shown by the OP.
In both cases, omitting the | Get-Disk part showed information about the disk image, which lacked a disk number, which is exactly what Get-Disk was complaining about. I suspect the image needs to be mounted for it to have a disk number.

How do I create a string dynamically in C#?

I am developing an application for managing Personal Hotspot of a Laptop
(Windows of Course).
I'm having a little difficulty in changing the Hotspot Name.
Here is my Code:
//For CMD Command
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "CMD.exe";
startInfo.CreateNoWindow = true;
//Reading User Input in textBox1:
string name = textBox1.Text;
//CMD Argument:..., Which is currently written wrong to make it Easy to
//understand the problem
startInfo.Arguments = "/C netsh wlan set hostednetwork ssid="name";
process.StartInfo = startInfo;
process.Start();
Here, In the Arguments line, the syntax is wrong. The value assigned to Arguments should be a single string. I need to incorporate name, that has a dynamic value, with the rest of the string that is constant. How do I do that?
Unless I'm missing something here, seems to me that a simple string concatenation would do it:
startInfo.Arguments = "/C netsh wlan set hostednetwork ssid=" + name;
There are several ways you can create a dynamic string:
For your specific case, Concatenation is the best choice as your string is very simple.The reason you would choose this method for your case is because it is very light as compared to other methods and has a clean enough syntax.
startInfo.Arguments = "/C netsh wlan set hostednetwork ssid=" + name;
For anything more complicated, string.Format is a popular go to. You would generally use it to combine more complex strings than your example. This tutorial covers the subject thoroughly.
startInfo.Arguments = string.Format("/C netsh wlan set hostednetwork ssid={0}", name);
The release of C#6.0 included a neat feature: interpolated strings. It is for the most part just syntactic sugar for string.Format, where the line below is turned into the line above at compile time. There are subtle differences, but those are not important in this thread.
startInfo.Arguments = $"/C netsh wlan set hostednetwork ssid={name}";
And lastly, if you need to change a string more than a few times (I usually use the rule of 5 - i.e. a string changes more than 5 times), I would use the StringBuilder class. A great application, among many others, would be a long loop that modifies a certain string each iteration. See This Tutorial.
In this case, the Garbage Collector will thank you for a using a StringBuilder!

How to call C++ exe with multiple parameters from command line using C# .net

i am performing offline operations on images taking image as a input with parameters and processing it in VTK C++ exe i am unable to pass parameters to C++ exe through C# program and retrive output .
please explain me with some example
If you just mean that you have a compiled C++ program (which we'll call "foo.exe", with path stored in string "exe_folder") and you want to call that with command line parameters (stored in string "exe_params") from C#, then the following should work:
string exe_params = "target_image.jpeg HOUGH_TRANSFORM"; // Or whatever params are appropriate.
string exe_full_path = Path.Combine(exe_folder, "foo.exe");
Process proc = System.Diagnostics.Process.Start(exe_full_path, exe_params);
https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx
Let say that your executable is called test.exe and it is in the test dir. For me, the following would be working:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C cd C:\\..test\\ && test.exe target_image.jpg yourtransformation";
process.StartInfo = startInfo;
process.Start();
If you have further problems, try setting the working directory of processStartInfo.

How to execute command-line arguments with parameters in C#?

I'm trying to compare two folders using a third party comparison tool called UltraCompare. The following line calls the program and opens two files... but this doesn't do anything except open them, plus it doesn't work properly for folders.
Process.Start("C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe",
textBoxContents1 + " " + textBoxContents2);
I'd like to use the following command line call which opens two folders, runs a comparison on them, and stores the results in output.txt: uc -d -dmf "c:\dir1" "c:\dir2" -o "c:\output.txt"
Also, I'd need to use variables for the folders instead of hardcoding the paths.
How can I work that into my C# code?
UPDATE 1:
I've modified my code according to your suggestions:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\\output2.txt",
textBoxContents1, textBoxContents2);
p.Start();
I'm wondering why the third line containing the arguments still doesn't work...
UPDATE 2:
My mistake. It is working now!! Just doesn't display the folders in UltraCompare but it is still writing and saving the output. Thanks guys!
You can use
yourProcess.StartInfo.Arguments = " .....";
Sample
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf {0} {1} -o c:\output.txt",textBoxContents1, textBoxContents2);
p.Start();
Process.Start(new ProcessStartInfo
{
FileName = #"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});
Make sure you use quotation marks for arguments, too!
If any of textBoxContents1 or textBoxContents2 contains spaces, you are busted!

How to use lame.exe in my application?

I capture audio files in the wave format in my Microsoft Surface application. Now for file size reasons, I'd like to convert the wave file into a mp3 file. I read in the Internet that a good possibility to do that is using lame.
But how can I call this exe file from my application? and how can I include it into my application?
Use Process class to call an external application:
string lameEXE = #"C:\path_of_lame\lame.exe";
string lameArgs = "-V2";
string wavFile = #"C:\my_wavs\input.wav";
string mp3File = #"C:\my_mp3s\output.mp3";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = lameEXE;
process.StartInfo.Arguments = string.Format(
"{0} {1} {2}",
lameArgs,
wavFile,
mp3File);
process.Start();
process.WaitForExit();
int exitCode = process.ExitCode;
You can call an executable from .NET by using the System.Diagnostics.Process class and related classes - see here for the documentation.
Lame has pretty robust command line arguments, which can be found here. You can pass command line arguments to the Process using the ProcessStartInfo.Arguments property.
public void mciConvertWavMP3(string fileName, bool waitFlag)
{
//maxLen is in ms (1000 = 1 second)
string outfile= "-b 32 --resample 22.05 -m m \"" + pworkingDir+fileName + "\" \"" + pworkingDir + fileName.Replace(".wav",".mp3") + "\"";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "\"" + pworkingDir + "lame.exe" + "\"";
psi.Arguments = outfile;
//psi.WorkingDirectory = pworkingDir;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
if (waitFlag)
{
p.WaitForExit();
// wait for exit of called application
}
}
Above code taken from here .
Depending on the usage, you can incorporate a Process.StartInfo object, control properties such as ShellExecute and also redirect any output from the application to (say) a log file or UI component.
To bundle the exe with your project, check this question from stackoverflow out. Personally, I'd go with the first suggestions:
There are several ways you could
accomplish this. First, you should add
program.exe to the project. You would
do this by right-clicking the project
in Visual Studio, and selecting Add >
Existing Item... Select program.exe,
and it will appear in the project.
Viewing its properties, you can set
"Copy to Output Directory" to "Copy
Always", and it will appear in your
output directory beside your
application.
If you stick to the above method, then reference lame.exe relatively ('....\Tools\Lame.exe' for example).
Finally, according to the official lame site : RareWares offers several compiled LAME versions, including modified versions featuring special functionality.
There is a DLL version of LAME, I would be surprised if you can't find a VB or C# example using it. Check this discussion thread: http://www.eggheadcafe.com/software/aspnet/31294459/-lameencdll-and-vbnet.aspx

Categories