使用ftp.exe与c# [英] Use ftp.exe with c#

查看:135
本文介绍了使用ftp.exe与c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 导航到 location

    li>

  2. 启动ftp.exe

  3. 打开服务器


  4. 密码

  5. 获取文件
  6. li>关闭
  7. 退出

我该怎么做?



请注意,我无法使用Net.FtpWebRequest进行这项特定任务。



有没有办法像一条线一样登录 ftp user:password @ host

>

C#:

  String ftpCmnds =打开+ folder.server +\r\\\
+ folder.cred.user +\r\\\
+ folder.cred.password +\r\
get+ file +\\ \\r\\\
close\r\\\
quit;

//这是我写的自定义方法:
Output.writeFile(basePath +\\+ Util.getDateFormated(reverseDate)+\\+ parentFolder +\\+ folder.server +\\,tmp.txt,ftpCmnds);
进程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();

使用(StreamWriter sw = p.StandardInput)
{
if(sw.BaseStream.CanWrite)
{
Console.WriteLine(Forcing Download从+ file +\\\
)的+ folder.server + folder.path +; log + =\r\\\
\r\\\
\t\t-强制从+ 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();






$ b

唯一坏的是 tmp.txt 文件在下载文件时可用,包含服务器的用户名和密码为纯文本。 : - /

我可以在名称后面追加一个随机字符串,但要使它更安全一些。


I need to get the following thing into the CMD in C#:

  1. navigate to location
  2. start ftp.exe
  3. open server
  4. user
  5. password
  6. get file
  7. close
  8. 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?

解决方案

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.

这篇关于使用ftp.exe与c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆