代码之家  ›  专栏  ›  技术社区  ›  Tony Borf

从目录自动ftp

  •  0
  • Tony Borf  · 技术社区  · 16 年前

    我想监视一个目录,并将所有放在那里的文件都转移到一个ftp位置。有人知道怎么用C语言做这个吗?

    谢谢

    编辑:有人知道一个好的客户机可以监控一个目录、ftp和放在其中的文件吗?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Joel Coehoorn    16 年前

    我结合了 System.IO.FileSystemWatcher System.Net.FtpWebRequest / FtpWebResponse 类。

    我们需要更多的信息来更加具体。

        2
  •  3
  •   Austin Salonen gmlacrosse    16 年前

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