Use ftp.exe with c# - 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.

Related

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:

using poweriso cmd commands from c sharp code

I am making a C# program that will make input file an .iso file.
For example if we write C:\Users\User\Desktpo\a.txt in the textbox that I created and give a destination path for example C:\ it has to create an iso file name a in C:\.
So I downloaded PowerISO and learn about piso.exe then I made some other research about using Process.Start(); in C# so I write these lines of code:
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "\"C:\PowerISO\piso.exe\"";
process.StartInfo.Arguments = str;
process.Start();
But this doesn't work.
Why?
EDIT: I am making an winform using Visual Studio 2017.
Please Note that
C:\PowerISO\piso.exe\
is not a valid C# path, the "\" before the P and the p is an escape indicater.
Either Add verbatim operator "#" before or escape it properly
edit:
In addition, you are adding extra double quotes to the FileName property.
You can check if the path is correct by asserting the file path with File.Exists method
Console.WriteLine(File.Exists(#"C:\PowerISO\piso.exe") ? "File exists." : "File does not exist.");
e.g
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = #"C:\PowerISO\piso.exe";
process.StartInfo.Arguments = str;
process.Start();
edit:
Verbatim operator will work, but escaping is better in my opinion.
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "C:\\PowerISO\\piso.exe";
process.StartInfo.Arguments = str;
process.Start();

Minecraft Launch with 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?

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..

C# pass path in argument

I'm starting cmd as a process in C# and I want to pass a file path in the argument. How to do it?
Process CommandStart = new Process();
CommandStart.StartInfo.UseShellExecute = true;
CommandStart.StartInfo.RedirectStandardOutput = false;
CommandStart.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
CommandStart.StartInfo.FileName = "cmd";
CommandStart.StartInfo.Arguments = "(here i want to put a file path to a executable) -arg bla -anotherArg blabla < (and here I want to put another file path)";
CommandStart.Start();
CommandStart.WaitForExit();
CommandStart.Close();
EDIT:
Process MySQLDump = new Process();
MySQLDump.StartInfo.UseShellExecute = true;
MySQLDump.StartInfo.RedirectStandardOutput = false;
MySQLDump.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
MySQLDump.StartInfo.FileName = "cmd";
MySQLDump.StartInfo.Arguments = "/c \"\"" + MySQLDumpExecutablePath + "\" -u " + SQLActions.MySQLUser + " -p" + SQLActions.MySQLPassword + " -h " + SQLActions.MySQLServer + " --port=" + SQLActions.MySQLPort + " " + SQLActions.MySQLDatabase + " \" > " + SQLActions.MySQLDatabase + "_date_" + date + ".sql";
MySQLDump.Start();
MySQLDump.WaitForExit();
MySQLDump.Close();
You need to put the file path in double quotes and use a verbatim string literal (#) as SLaks mentioned.
CommandStart.StartInfo.Arguments = #"""C:\MyPath\file.exe"" -arg bla -anotherArg";
Example with an OpenFileDialog
using(OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filePath = "\"" + ofd.FileName + "\"";
//..set up process..
CommandStart.StartInfo.Arguments = filePath + " -arg bla -anotherArg";
}
}
Update to comment
You can format your string using String.Format.
string finalPath = String.Format("\"{0}{1}_date_{2}.sql\"", AppDomain.CurrentDomain.BaseDirectory, SQLActions.MySQLDatabase, date);
Then pass finalPath into the arguments.

Categories