【发布时间】:2012-05-06 05:50:29
【问题描述】:
我想监视一个目录并将其中的所有文件通过 FTP 传输到 FTP 位置。有谁知道如何在 C# 中做到这一点?
谢谢
编辑:任何人都知道可以监控目录和 FTP 以及放置在其中的文件的好客户端吗?
【问题讨论】:
我想监视一个目录并将其中的所有文件通过 FTP 传输到 FTP 位置。有谁知道如何在 C# 中做到这一点?
谢谢
编辑:任何人都知道可以监控目录和 FTP 以及放置在其中的文件的好客户端吗?
【问题讨论】:
System.IO.FileSystemWatcher 和 System.Net.FtpWebRequest/FtpWebResponse 类的组合。
我们需要更多信息才能更具体。
【讨论】:
当与 FileSystemWatcher 结合使用时,此代码是一种将文件上传到服务器的快速而肮脏的方式。
public static void Upload(string ftpServer, string directory, string file)
{
//ftp command will be sketchy without this
Environment.CurrentDirectory = directory;
//create a batch file for the ftp command
string commands = "\n\nput " + file + "\nquit\n";
StreamWriter sw = new StreamWriter("f.cmd");
sw.WriteLine(commands);
sw.Close();
//start the ftp command with the generated script file
ProcessStartInfo psi = new ProcessStartInfo("ftp");
psi.Arguments = "-s:f.cmd " + ftpServer;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
File.Delete(file);
File.Delete("f.cmd");
}
【讨论】: