开发者

Auto FTP from a Directory

I want to monitor a directory and FTP any files that are place there to an FTP location. does anyone know how to do this in c#?

Thanks

EDIT: Anyone know of a good client that开发者_如何学C can monitor a directory and FTP and files placed in it?


I combination of the System.IO.FileSystemWatcher and System.Net.FtpWebRequest/FtpWebResponse classes.

We need more information to be more specific.


When used in conjunction with the FileSystemWatcher, this code is a quick and dirty way to upload a file to a server.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜