2009-11-16 75 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"); 
}