我需要在C#中将以下内容添加到CMD中:
我如何做到这一点?
请注意,我无法使用Net.FtpWebRequest执行此特定任务。
有没有办法在一行中登录,例如 ftp user:password @ host ?
答案 0 :(得分:2)
尝试调用bat文件?
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();
调用ftp.bat文件
Ftp.Bat文件包含...
ftp -s:commands.ftp
然后在你的commands.ftp
中open <server_address>
<userid>
<password>
recv <source_file> <dest_file>
bye
或类似的东西。
答案 1 :(得分:1)
我选择的解决方案:
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();
}
}
唯一的&#34;坏&#34;事情是 tmp.txt 文件,它在下载文件所需的时间内可用,包含服务器的用户名和密码作为纯文本。 : - /
我可以在名称后附加一个随机字符串,但要使它更安全一些。