0

I am coding a c# winform app which runs minecraft using cmd and logs live output to a textbox, like a console in minecraft launcher (btw minecraft logs output to cmd), there are some posts about this but nothing works for me.

Edit : It doesnt runs the command but close it still runs the cmd, not the command though

What i've tried:

string Username = Username_box.Text;
string RamAmout = Ram_Amout.Text;

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "cmd.exe";
// startInfo.WindowStyle = ProcessWindowStyle.Hidden;
// startInfo.Arguments = "/C echo nothing";
process.OutputDataReceived += (s, e) => { ConsoleLog.Text = e.Data; };
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

process.BeginOutputReadLine();

process.StandardInput.WriteLine("cd bin & cd clients & cd mcsave & ..\\azul-java\\bin\\java.exe -noverify -Xms" + RamAmout + "g " + " -Xmx" + RamAmout + "g " + "-Djava.library.path=..\\1.8.9-natives -cp \"..\\189\\vanilla.jar;lwjgl.jar;lwjgl_util.jar\" net.minecraft.client.main.Main -uuid fc5bc365-aedf-30a8-8b89-04e462e29bde -accessToken yes -version 1 --username " + Username");

process.WaitForExit();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Minh Long
  • 1
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 09 '23 at 08:56
  • 1
    Delete that last ". It makes your code not compile. Maybe consider string interpolation to mitigate that type of error. (There may well be additional issues, I stopped there) – Fildor Aug 09 '23 at 09:10
  • @Fildor-standswithMods What dot, the dot in process.StandardInput.WriteLine ? – Minh Long Aug 09 '23 at 09:12
  • 1
    No, the last " in `...--username " + Username");` – Fildor Aug 09 '23 at 09:13
  • It is easy to make that error when you concat strings like that. Maybe consider using `$" ... --username {Username}");` (The ... of course are the rest of the string put into interpolation syntax accordingly. I just used Username as an example) – Fildor Aug 09 '23 at 09:14
  • Also the string expression can be simplified at several places. E.g., `"g " + " -Xmx"` is the same as `"g -Xmx"`. – Olivier Jacot-Descombes Aug 09 '23 at 09:22
  • And _of course_ as always: Why not start "java.exe" directly instead of cmd.exe? Which is probably the reason you don't get output: java.exe writes to cmd, cmd.exe writes ... nothing. – Fildor Aug 09 '23 at 09:28
  • 1
    ^^ I don't have hard numbers to back up the claim but: subjective felt experience with SO Questions that include Process.Start is - whenever "cmd.exe" is being started, in 9 out of 10 cases it actually shouldn't. So, that alone rings some bells. – Fildor Aug 09 '23 at 09:53
  • @Fildor-standswithMods Edit : i misclicked lol, But i use cmd to cd to the path of minecraft save – Minh Long Aug 09 '23 at 10:47
  • [link](https://cdn.discordapp.com/attachments/1128241610103201792/1138786034373640212/cmd_XQBwhhYZgR.png) If its work it would show smth like this (i forgot to add the logging implementation so thats why its so short) – Minh Long Aug 09 '23 at 10:50
  • The C# [Process Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process) is a C# wrapper class for the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). `CreateProcess` is called by `cmd.exe` to run an executable like `java.exe`. I do not understand why a C# coded application does not make use of the `Process` class to run directly `java.exe`. There is absolutely no need to run `cmd.exe` just to run `java.exe`. – Mofi Aug 09 '23 at 16:45
  • `CreateProcess` has the function parameter `lpCurrentDirectory` to define the current working directory for the executable. Why is [ProcessStartInfo.WorkingDirectory Property](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory) not used to define the current working directory for `java.exe`? What let you think the process starting the C# coded program calls `CreateProcess` with `lpCurrentDirectory` being the directory containing the program executable? There is no guarantee that the parent process does that. – Mofi Aug 09 '23 at 16:51
  • It looks like this is an [XY problem](https://xyproblem.info/) caused by spending not enough time in the research on how to run an executable from within a C# code application and captures its output to standard output and perhaps also standard error to do whatever should be done with that data. There should be read at least the documentation pages of the used classes and think about what could be better for the task than running `cmd.exe` to run `java.exe`. There can be found also hundreds of examples in world wide web and on Stack Overflow related to this task as far as I understood it. – Mofi Aug 09 '23 at 16:57
  • @Mofi Because the java file is not in the root of the application – Minh Long Aug 10 '23 at 03:05
  • The current working directory can be any directory and `java.exe` can be passed to `CreateProcess` via the `Process` class with its absolute path, perhaps first determined by the C# coded program by getting the program files path of the C# coded program and concatenated with the relative path. C# offers all the functions needed to do that. See [How can I get the application's path in a .NET console application?](https://stackoverflow.com/questions/837488/) and [Getting assembly location: GetAssembly().Location or GetExecutingAssembly().Location?](https://stackoverflow.com/a/27060089/3074564) – Mofi Aug 10 '23 at 04:47
  • Read also the Microsoft documentations for [Path.GetFullPath Method](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath) and [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file). `cmd.exe` uses Windows library functions. The same functions in the same libraries can be used also by a program coded in C#. There is never the need for a C# coded program to run `cmd.exe` or any other executable in `%SystemRoot%\System32`. – Mofi Aug 10 '23 at 04:51

0 Answers0