2009-11-16 62 views
0

我想监视一个目录并将任何放置在FTP位置的文件都FTP给FTP。有谁知道如何在C#中做到这一点?从目录中自动创建FTP

感谢

编辑:任何人都知道一个好的客户端,可以监控一个目录,FTP和文件摆在那呢?

回答

3

我组合System.IO.FileSystemWatcherSystem.Net.FtpWebRequest/FtpWebResponse类。

我们需要更多信息才能更具体。

+0

1.观察任何新文件的特定目录(“FTP-IN”)。 2.接收该文件并将其FTP到Web服务器。 3.将文件从“FTP-IN”目录移动到另一个目录。 4.回到#1。 – 2009-11-19 12:48:28

2

与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"); 
}