日期:2014-05-18 浏览次数:20868 次
using System; using System.Net; using System.Threading; using System.IO; namespace Examples.System.Net { public class FtpState { private ManualResetEvent wait; private FtpWebRequest request; private string fileName; private Exception operationException = null; string status; public FtpState() { wait = new ManualResetEvent(false); } public ManualResetEvent OperationComplete { get {return wait;} } public FtpWebRequest Request { get {return request;} set {request = value;} } public string FileName { get {return fileName;} set {fileName = value;} } public Exception OperationException { get {return operationException;} set {operationException = value;} } public string StatusDescription { get {return status;} set {status = value;} } } public class AsynchronousFtpUpLoader { // Command line arguments are two strings: // 1. The url that is the name of the file being uploaded to the server. // 2. The name of the file on the local machine. // public static void Main(string[] args) { // Create a Uri instance with the specified URI string. // If the URI is not correctly formed, the Uri constructor // will throw an exception. ManualResetEvent waitObject; Uri target = new Uri (args[0]); string fileName = args[1]; FtpState state = new FtpState(); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target); request.Method = WebRequestMethods.Ftp.UploadFile; // This example uses anonymous logon. // The request is anonymous by default; the credential does not have to be specified. // The example specifies the credential only to // control how actions are logged on the server. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); // Store the request in the object that we pass into the // asynchronous operations. state.Request = request; state.FileName = fileName; // Get the event to wait on. waitObject = state.OperationComplete; // Asynchronously get the stream for the file contents. request.BeginGetRequestStream( new AsyncCallback (EndGetStreamCallback), state ); // Block the current thread until all operations are complete. waitObject.WaitOne(); // The operations either completed or threw an exception. if (state.OperationException != null) { throw state.OperationException; } else { Console.WriteLine("The operation completed - {0}", state.StatusDescription); } } private static void EndGetStreamCallback(IAsyncResult ar) { FtpState state = (FtpState) ar.AsyncState; Stream requestStream = null; // End the asynchronous call to get the request stream. try { requestStream = state.Request.EndGetRequestStream(ar); // Copy the file contents to the request stream. const int bufferLength = 2048; byte[] buffer