}//private static void EndGetResponseCallback()
 
 ///<summary>WebClientDownload</summary>
 ///<remarks>http://www.codeguru.com/Csharp/Csharp/cs_network/configurationfilesinis/article.php/c9061__2/</remarks>
 public static void WebClientDownload
 (
  ref UtilityFTPArgument  utilityFTPArgument,
  ref string              exceptionMessage
 )
 {
  WebClient webClient;
  try
  {
   webClient = new WebClient();
   //webClient.Credentials = new NetworkCredential("anonymous", "*****@*****.**");
   webClient.DownloadFile( utilityFTPArgument.uriSource, utilityFTPArgument.filenameTarget );
  }//try
  catch ( WebException exception )
  {
  	exceptionMessage = "WebException: " + exception.Message;
  }//catch ( WebException exception )	 
  catch ( SecurityException exception )
  {
  	exceptionMessage = "SecurityException: " + exception.Message;
  }//catch ( SecurityException exception )	 
  catch ( Exception exception )
  {
  	exceptionMessage = "Exception: " + exception.Message;
  }//catch ( Exception exception )	 
 }//public static void WebClientDownload
  /// <summary>Main</summary>
  public static void Main
  ( 
   string[] argv
  )
  {
   Boolean                       booleanParseCommandLineArguments  =  false;
   string                        exceptionMessage                  =  null;     
   string                        filenameApplication               =  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
   UtilityFTPArgument            utilityFTPArgument                =  null;
   
   utilityFTPArgument = new UtilityFTPArgument();
   
   booleanParseCommandLineArguments =  UtilityParseCommandLineArgument.ParseCommandLineArguments
   ( 
    argv, 
    utilityFTPArgument
   );

   if ( booleanParseCommandLineArguments == false )
   {
    // error encountered in arguments. Display usage message
    System.Console.Write
    (
     UtilityParseCommandLineArgument.CommandLineArgumentsUsage( typeof ( UtilityFTPArgument ) )
    );
    return;
   }//if ( booleanParseCommandLineArguments  == false )

   #if (DEBUG)
    System.Console.WriteLine
    (
     "Filename Application: {0}",
     filenameApplication
    );
   #endif

   #if (DEBUG)
    System.Console.WriteLine
    (
     "Argument FilenameSource: {0} | URISource: {1} | FilenameTarget: {2} | URITarget: {3}",
     utilityFTPArgument.filenameSource,
     utilityFTPArgument.uriSource,     
     utilityFTPArgument.filenameTarget,
     utilityFTPArgument.uriTarget
    );
   #endif
   
   Stub
   (
    ref  utilityFTPArgument,
    ref  exceptionMessage
   );
    
  }//Main()  
  }//Main()  

  /// <summary>Stub</summary>
  public static void Stub
  (
   ref UtilityFTPArgument  utilityFTPArgument,
   ref string              exceptionMessage
  )
  {  
   if 
   ( 
    !string.IsNullOrEmpty( utilityFTPArgument.filenameSource ) &&
    !string.IsNullOrEmpty( utilityFTPArgument.uriTarget 
   )
   )
   {
    FTPUploadFile
    (
     ref utilityFTPArgument,
     ref exceptionMessage
    );
   }//if ( !string.IsNullOrEmpty( utilityFTPArgument.filenameSource ) && !string.IsNullOrEmpty( utilityFTPArgument.uriTarget )

   if 
   ( 
    !string.IsNullOrEmpty( utilityFTPArgument.uriSource ) &&
    !string.IsNullOrEmpty( utilityFTPArgument.filenameTarget )
   )
   {

    FTPWebRequestResponse
    (
     ref utilityFTPArgument,
     ref exceptionMessage
    );
   }//if ( !string.IsNullOrEmpty( utilityFTPArgument.uriSource ) && !string.IsNullOrEmpty( utilityFTPArgument.filenameTarget ) )

   if 
   ( 
    !string.IsNullOrEmpty( utilityFTPArgument.uriSource ) &&
    string.IsNullOrEmpty( utilityFTPArgument.filenameTarget )
   )
   {

    FTPWebRequestResponse
    (
     ref utilityFTPArgument,
     ref exceptionMessage
    );
   }//if ( !string.IsNullOrEmpty( utilityFTPArgument.uriSource ) && string.IsNullOrEmpty( utilityFTPArgument.filenameTarget ) )
   
  }//Stub()
示例#4
0
  /// <summary>Main</summary>
  public static void Main
  ( 
   string[] argv
  )
  {
   Boolean                       booleanParseCommandLineArguments  =  false;
   string                        exceptionMessage                  =  null;     
   UtilityFTPArgument            utilityFTPArgument                =  null;
   
   utilityFTPArgument = new UtilityFTPArgument();
   
   booleanParseCommandLineArguments =  UtilityParseCommandLineArgument.ParseCommandLineArguments
   ( 
    argv, 
    utilityFTPArgument
   );

   if ( booleanParseCommandLineArguments == false )
   {
    // error encountered in arguments. Display usage message
    System.Console.Write
    (
     UtilityParseCommandLineArgument.CommandLineArgumentsUsage( typeof ( UtilityFTPArgument ) )
    );
    return;
   }//if ( booleanParseCommandLineArguments  == false )

   #if (DEBUG)
    System.Console.WriteLine
    (
     "Argument FilenameSource: {0} | URITarget: {1}",
     utilityFTPArgument.filenameSource,
     utilityFTPArgument.uriTarget
    );
   #endif

   FTPUploadFile
   (
    ref utilityFTPArgument,
    ref exceptionMessage
   );
   
  }//Main  
  }//public String URITarget

  /// <summary>ButtonSubmit_Click().</summary>
  public void ButtonSubmit_Click
  (
   Object sender, 
   EventArgs e
  )
  {

   String               exceptionMessage      =  null;

   String               filenameSource        =  null;
   String               uriTarget             =  null;

   UtilityFTPArgument   utilityFTPArgument    =  null;
   
   filenameSource                             =  FilenameSource;
   uriTarget                                  =  URITarget;

   /*
   if( sender == ButtonSubmit )
   {
    Response.Write ( "ButtonSubmit" );
    Feedback = "ButtonSubmit";
   }
   */

   utilityFTPArgument = new UtilityFTPArgument
   (
    filenameSource,
    null, //filenameTarget
    null, //uriSource
    uriTarget
   );

   UtilityFTP.FTPUploadFile
   (
    ref utilityFTPArgument,
    ref exceptionMessage
   );

   Feedback = exceptionMessage;
   
  }//public void ButtonSubmit_Click()
  }//public static void WebClientDownload

  ///<summary>FTPWebRequestResponse</summary>
  ///<remarks>http://www.codeguru.com/Csharp/Csharp/cs_network/configurationfilesinis/article.php/c9061__2/</remarks>
  public static void FTPWebRequestResponse
  (
   ref UtilityFTPArgument  utilityFTPArgument,
   ref string              exceptionMessage
  )
  {

   FtpWebResponse  ftpWebResponse;
   FtpWebRequest   ftpWebRequest;
   Stream          stream;
   StreamReader    streamReader;

   try
   {
    // Set up the FTP connection
    ftpWebRequest = (FtpWebRequest) WebRequest.Create( utilityFTPArgument.uriSource );
    //ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**");

    // Initiate the request and read the response
    ftpWebResponse  =  (FtpWebResponse) ftpWebRequest.GetResponse();
    stream          =  ftpWebResponse.GetResponseStream();
    streamReader    =  new StreamReader( stream, System.Text.Encoding.UTF8);

    // Display the file contents
    System.Console.WriteLine( streamReader.ReadToEnd() );
   }//try
   catch ( NotSupportedException exception )
   {
    exceptionMessage = "NotSupportedException: " + exception.Message;
   }//catch ( NotSupportedException exception )	 
   catch ( ArgumentNullException exception )
   {
    exceptionMessage = "ArgumentNullException: " + exception.Message;
   }//catch ( ArgumentNullException exception )	 
   catch ( SecurityException exception )
   {
    exceptionMessage = "SecurityException: " + exception.Message;
   }//catch ( SecurityException exception )	 
   catch ( UriFormatException exception )
   {
    exceptionMessage = "UriFormatException: " + exception.Message;
   }//catch ( UriFormatException exception )	 
   catch ( System.Net.WebException exception )
   {
    exceptionMessage = "System.Net.WebException: " + exception.Message;
   }//catch ( System.Net.WebException exception )	 
   catch ( OutOfMemoryException exception )
   {
    exceptionMessage = "OutOfMemoryException: " + exception.Message;
   }//catch ( OutOfMemoryException exception )	 
   catch ( System.InvalidOperationException exception )
   {
    exceptionMessage = "System.InvalidOperationException: " + exception.Message;
   }//catch ( System.InvalidOperationException exception )	 
   catch ( IOException exception )
   {
    exceptionMessage = "IOException: " + exception.Message;
   }//catch ( IOException exception )	 
   catch ( Exception exception )
   {
    exceptionMessage = "Exception: " + exception.Message;
   }//catch ( Exception exception )	 
  }//public static void FTPWebRequestResponse( ref UtilityFTPArgument  utilityFTPArgument, ref string exceptionMessage )
  }//Stub()

  /// <summary>FTPUploadFile</summary>
  public static void FTPUploadFile
  (
   ref UtilityFTPArgument  utilityFTPArgument,
   ref string              exceptionMessage
  )
  {
   string            fileName;
   FtpWebRequest     request;
   HttpContext       httpContext  =  HttpContext.Current;
   ManualResetEvent  waitObject;
   Uri               target;
   FtpState          state;
           
   try
   {

    // Create a Uri instance with the specified URI string.
    // If the URI is not correctly formed, the Uri constructor
    // will throw an exception.
            
    target = new Uri ( utilityFTPArgument.uriTarget );

    fileName = utilityFTPArgument.filenameSource;
    
    state = new FtpState();
     
    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","*****@*****.**");
            
    // 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
    {
     System.Console.WriteLine("The operation completed - {0}", state.StatusDescription);
    }
    
   }//try
   catch ( Exception exception )
   {
    exceptionMessage = "Exception: " + exception.Message;
   }//catch ( Exception exception )

   if ( exceptionMessage != null )
   {
    if ( httpContext == null )
    {
     System.Console.WriteLine( exceptionMessage );
    }//if ( httpContext == null )
    else
    {
     //httpContext.Response.Write( exceptionMessage );
    }//else 
   }//if ( exceptionMessage != null )
     
  }//public static void FTPUploadFile()