// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; namespace E { class MainClass { public static void Upload(string localfile, string ftpurl, bool bin) { FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpurl); req.Method = WebRequestMethods.Ftp.UploadFile; req.UseBinary = bin; if(bin) { Stream instm = new FileStream(localfile, FileMode.Open, FileAccess.Read); Stream outstm = req.GetRequestStream(); byte[] b = new byte[10000]; int n; while((n = instm.Read(b, 0, b.Length)) > 0) { outstm.Write(b, 0, n); } instm.Close(); outstm.Close(); } else { StreamReader sr = new StreamReader(localfile); StreamWriter sw = new StreamWriter(req.GetRequestStream()); string line; while((line = sr.ReadLine()) != null) { sw.WriteLine(line); } sr.Close(); sw.Close(); } FtpWebResponse resp = (FtpWebResponse)req.GetResponse(); Console.WriteLine(resp.StatusCode); } public static void Main(string[] args) { Upload(@"C:\X.java", "ftp://anonymous:xxxxxx@arnepc3/X.java", false); Upload(@"C:\X.class", "ftp://anonymous:xxxxxx@arnepc3/X.class", true); } } }