Minecraft Launch with C# - c#

i want launch minecraft client with c# but my code doesnt work:
private void Startmc(String a, String b)
{
string javafolder = GetJavaInstallationPath();
string filepath = Path.Combine(javafolder, #"bin\");
Environment.SetEnvironmentVariable("APPDATA", kurulumdosyasi);
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "javaw";
psi.CreateNoWindow = true;
psi.Arguments = "-cp \"" + filepath + ".jar;" + filepath + "lwjgl.jar;" + filepath + "lwjgl_util.jar;" + filepath + "jinput.jar;\" ";
psi.Arguments += "\"-Djava.library.path=" + filepath + "natives\" -Xmx1024M -Xms512M net.minecraft.client.main.Main " + a + " " + b;
p.StartInfo = psi;
p.Start();
}
after click to login button nothing happen

Can't you just open a Process() from the main launcher in the install directory (C:\Program Files (x86)\Minecraft\MinecraftLauncher.exe)? It looks like you're trying to bypass the default behavior...what are you working on?

Related

The process tried to write to a nonexistent pipe when running cmd.exe using c#

I tried to call a .exe program I have calling cmd from a c# script that generates a command. When I run the code I get the output the process tried to write to a nonexistent pipe. But when I run said command using a cmd open from Windows it runs perfectly. What I am doing wrong? I am using Visual Studio 2022.
SilentMkdir(baseDir + "/00_CameraInit");
string binName =pre + binDir + "\\aliceVision_cameraInit.exe";
string dstDir =pre + baseDir + "/00_CameraInit\\";
string cmdLine = binName;
cmdLine = cmdLine + " --defaultFieldOfView 45.0 --verboseLevel info --sensorDatabase \"\" --allowSingleView 1";
cmdLine = cmdLine + " --imageFolder \"" + pre + srcImageDir + "\"";
cmdLine = cmdLine + " --output \"" + dstDir + "cameraInit.sfm\"";
Console.WriteLine(cmdLine);
var processInfo = new ProcessStartInfo("cmd.exe", cmdLine);
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.CreateNoWindow = false;
//processInfo.RedirectStandardOutput = !string.IsNullOrEmpty("output.txt");
int exitCode = -1;
string output = null;
try
{
var process = Process.Start(processInfo);
process.Start();
output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
process.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine("Run error" + e.ToString()); // or throw new Exception
}

How can I take ownership of a file in c# using takeown or icacls?

I know there are other posts on the matter but none have presented me with a working solution.
I'm trying to take ownership of files under C:\Windows\Media so I can replace them with my own. Currently I've been trying to do that by running either takeown or icacls in CMD, but neither seem to work. The takeown method actually returns success from CMD but I still get access denied when trying to delete the file. With the icacls method I get "the handle is invalid" and still get access denied.
The program is running as administator fyi.
And also in the icacls method changing Environment.Username to "Administrators" doesn't change anything.
Any help is appreciated!
Here is the code for the takeown method:
string[] soundFiles;
soundFiles = Directory.GetFiles(#"C:/Windows/Media", "*.wav");
//string cmdargs = #"/c takeown /F C:\Windows\Media";
string shortcmdargs = #"/c takeown /F ";
//Process.Start("CMD.exe", cmdargs);
string output = "null";
foreach(string file in soundFiles)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = shortcmdargs + #"C:\Windows\Media\" + Path.GetFileName(file);
p.Start();
output = p.StandardOutput.ReadToEnd();
System.IO.File.Delete(file);
System.IO.File.Copy("neco.wav", file);
}
catch(Exception exce)
{
MessageBox.Show("Output: " + output + "\neException: " + exce);
}
}
And here is what I get when running it:
Here is the code for the icacls method:
string[] soundFiles;
soundFiles = Directory.GetFiles(#"C:/Windows/Media", "*.wav");
string cmdargs = #"/c takeown /F C:\Windows\Media";
string shortcmdargs = #"/c takeown /F ";
Process.Start("CMD.exe", cmdargs);
string output = "null";
foreach(string file in soundFiles)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"/c icacls " + #"C:\Windows\Media\" + Path.GetFileName(file) + " /grant " + Environment.UserName + ":(OI)(CI)F /T";
p.Start();
output = p.StandardOutput.ReadToEnd();
System.IO.File.Delete(file);
System.IO.File.Copy("neco.wav", file);
}
catch(Exception exce)
{
MessageBox.Show("Output: " + output + "\neException: " + exce);
}
}
And here is what I get with it:

Hide cmd while conversion from WAV to MP3 using lame

The conversion is successfully worked but during conversion command prompt is open for a fraction of second..I dont want to show it
The code is:
string[] name = new string[all_audio_name.Count()];
string mpfile;
System.Diagnostics.Process pro = null;
mpfile = Path.GetFileName(all_audio_name[j]).Replace(".wav", ".mp3");
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
string outfile = "-b 32 --resample 22.05 -m j " + "\"" + new_path + "\\" + Path.GetFileName(all_audio_name[j]) + "\"" + " " + "\"" + new_path + "\\" + Path.GetFileNameWithoutExtension(all_audio_name[j]) + ".mp3" + "\"";
psi.FileName = HttpContext.Current.Server.MapPath("~/Dependencies/lame.exe");
psi.Arguments = outfile;
psi.UseShellExecute = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pro = System.Diagnostics.Process.Start(psi); //IGNORE VARIABLE NAME
after System.Diagnostics.Process.Start(psi); line cmd prompt appear..plz suggest me to hide it..

Use ftp.exe with c#

I need to get the following thing into the CMD in C#:
navigate to location
start ftp.exe
open server
user
password
get file
close
quit
How do I accomplish that?
Please mind that I can not use Net.FtpWebRequest for this particular task.
Is there a way to log in in one line like ftp user:password#host?
Try calling a bat file?
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c e:\test\ftp.bat";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
Call ftp.bat file
Ftp.Bat file contains...
ftp -s:commands.ftp
Then in your commands.ftp
open <server_address>
<userid>
<password>
recv <source_file> <dest_file>
bye
Or something similar.
The solution I went with:
C#:
String ftpCmnds = "open " + folder.server + "\r\n" + folder.cred.user + "\r\n" + folder.cred.password + "\r\nget " + file + "\r\nclose\r\nquit";
//This is a custom method that I wrote:
Output.writeFile(basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\", "tmp.txt", ftpCmnds);
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.WindowStyle = ProcessWindowStyle.Hidden;
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
Console.WriteLine("Forcing Download from " + folder.server + folder.path + " of " + file + "\n"); log += "\r\n\r\n\t\t- Forcing Download from " + folder.server + folder.path + file + "\tto\t" + basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\" + file;
sw.WriteLine("cd " + basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\");
sw.WriteLine("ftp -s:tmp.txt");
sw.WriteLine("del tmp.txt");
p.Close();
}
}
The only "bad" thing is that the tmp.txt file, which is availiable for the time it requires to download the file, contains the username and password of the server as plain text. :-/
I could append a random String to the name though to make it a little more secure.

How would I compress a folder with 7Zip in C#?

I want to compress a folder into a file with the .7z extension, with 7zip.
I would like to know how I would do this, because I'm not sure (which is why I'd asking.)
This is in C#.
Links to pages or sample code would be helpful.
code to zip or unzip file using 7zip
this code is used to zip a folder
public void CreateZipFolder(string sourceName, string targetName)
{
// this code use for zip a folder
sourceName = #"d:\Data Files"; // folder to be zip
targetName = #"d:\Data Files.zip"; // zip name you can change
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"D:\7-Zip\7z.exe";
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
this code is used to zip a file
public void CreateZip(string sourceName, string targetName)
{
// file name to be zip , you must provide file name with extension
sourceName = #"d:\ipmsg.log";
// targeted file , you can change file name
targetName = #"d:\ipmsg.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"D:\7-Zip\7z.exe";
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
this code is used for unzip
public void ExtractFile(string source, string destination)
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string zPath = #"D:\7-Zip\7zG.exe";
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) { }
}
I agree that this is a duplicate, but I've used the demo from this codeproject before and it is very helpful:
http://www.codeproject.com/Articles/27148/C-NET-Interface-for-7-Zip-Archive-DLLs
Scroll down the page for the demo and good luck!
Here fileDirPath is the path to my folder which has all my files and preferredPath is the path where I want my .zip file to be.
eg:
var fileDirePath = #“C:\Temp”;
var prefferedPath = #“C:\Output\results.zip”;
private void CreateZipFile(string fileDirPath, string prefferedPath)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\Program Files\7-Zip\7z.exe";
p.Arguments = "a \"" + prefferedPath + "\" \"" + fileDirPath + "\"";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
return;
}

Categories