tags:

views:

83

answers:

2

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  A: 

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

We need more information to be more specific.

Joel Coehoorn
1. Watch a specific directory("FTP-IN") for any new files.2. Take that file and FTP it to a web server.3. Move the file from the "FTP-IN" directory to another directory.4. go back to #1.
Tony Borf
+2  A: 

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