Auto FTP from a Directory

2019-08-26 09:13发布

问题:

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 can monitor a directory and FTP and files placed in it?

回答1:

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

We need more information to be more specific.



回答2:

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


标签: c# ftp