5 using System.Net.Sockets;
6 using System.Diagnostics;
7 using System.Runtime.Remoting;
8 using System.Runtime.Remoting.Messaging;
11 * FTP Client library in C#
12 * Author: Jaimon Mathew
13 * mailto:jaimonmathew@rediffmail.com
14 * http://www.csharphelp.com/archives/archive9.html
16 * Addapted for use by Dan Glass 07/03/03
20 namespace ReactOS.CustomRevisionAction
23 public class FtpClient
26 public class FtpException : Exception
28 public FtpException(string message) : base(message){}
29 public FtpException(string message, Exception innerException) : base(message,innerException){}
32 private static int BUFFER_SIZE = 512;
33 private static Encoding ASCII = Encoding.ASCII;
35 private bool verboseDebugging = false;
38 private string server = "localhost";
39 private string remotePath = ".";
40 private string username = "anonymous";
41 private string password = "anonymous@anonymous.net";
42 private string message = null;
43 private string result = null;
45 private int port = 21;
46 private int bytes = 0;
47 private int resultCode = 0;
49 private bool loggedin = false;
50 private bool binMode = false;
52 private Byte[] buffer = new Byte[BUFFER_SIZE];
53 private Socket clientSocket = null;
55 private int timeoutSeconds = 10;
58 /// Default contructor
66 /// <param name="server"></param>
67 /// <param name="username"></param>
68 /// <param name="password"></param>
69 public FtpClient(string server, string username, string password)
72 this.username = username;
73 this.password = password;
78 /// <param name="server"></param>
79 /// <param name="username"></param>
80 /// <param name="password"></param>
81 /// <param name="timeoutSeconds"></param>
82 /// <param name="port"></param>
83 public FtpClient(string server, string username, string password, int timeoutSeconds, int port)
86 this.username = username;
87 this.password = password;
88 this.timeoutSeconds = timeoutSeconds;
93 /// Display all communications to the debug log
95 public bool VerboseDebugging
99 return this.verboseDebugging;
103 this.verboseDebugging = value;
107 /// Remote server port. Typically TCP 21
121 /// Timeout waiting for a response from server, in seconds.
127 return this.timeoutSeconds;
131 this.timeoutSeconds = value;
135 /// Gets and Sets the name of the FTP server.
137 /// <returns></returns>
150 /// Gets and Sets the port number.
152 /// <returns></returns>
153 public int RemotePort
165 /// GetS and Sets the remote directory.
167 public string RemotePath
171 return this.remotePath;
175 this.remotePath = value;
180 /// Gets and Sets the username.
182 public string Username
186 return this.username;
190 this.username = value;
194 /// Gets and Set the password.
196 public string Password
200 return this.password;
204 this.password = value;
209 /// If the value of mode is true, set binary mode for downloads, else, Ascii mode.
211 public bool BinaryMode
219 if ( this.binMode == value ) return;
222 sendCommand("TYPE I");
225 sendCommand("TYPE A");
227 if ( this.resultCode != 200 ) throw new FtpException(result.Substring(4));
231 /// Login to the remote server.
235 if ( this.loggedin ) this.Close();
237 Debug.WriteLine("Opening connection to " + this.server, "FtpClient" );
239 IPAddress addr = null;
240 IPEndPoint ep = null;
244 this.clientSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
245 addr = Dns.Resolve(this.server).AddressList[0];
246 ep = new IPEndPoint( addr, this.port );
247 this.clientSocket.Connect(ep);
252 if ( this.clientSocket != null && this.clientSocket.Connected ) this.clientSocket.Close();
254 throw new FtpException("Couldn't connect to remote server",ex);
259 if(this.resultCode != 220)
262 throw new FtpException(this.result.Substring(4));
265 this.sendCommand( "USER " + username );
267 if( !(this.resultCode == 331 || this.resultCode == 230) )
270 throw new FtpException(this.result.Substring(4));
273 if( this.resultCode != 230 )
275 this.sendCommand( "PASS " + password );
277 if( !(this.resultCode == 230 || this.resultCode == 202) )
280 throw new FtpException(this.result.Substring(4));
284 this.loggedin = true;
286 Debug.WriteLine( "Connected to " + this.server, "FtpClient" );
288 this.ChangeDir(this.remotePath);
292 /// Close the FTP connection.
296 Debug.WriteLine("Closing connection to " + this.server, "FtpClient" );
298 if( this.clientSocket != null )
300 this.sendCommand("QUIT");
307 /// Return a string array containing the remote directory's file list.
309 /// <returns></returns>
310 public string[] GetFileList()
312 return this.GetFileList("*.*");
316 /// Return a string array containing the remote directory's file list.
318 /// <param name="mask"></param>
319 /// <returns></returns>
320 public string[] GetFileList(string mask)
322 if ( !this.loggedin ) this.Login();
324 Socket cSocket = createDataSocket();
326 this.sendCommand("NLST " + mask);
328 if(!(this.resultCode == 150 || this.resultCode == 125)) throw new FtpException(this.result.Substring(4));
332 DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
334 while( timeout > DateTime.Now )
336 int bytes = cSocket.Receive(buffer, buffer.Length, 0);
337 this.message += ASCII.GetString(buffer, 0, bytes);
339 if ( bytes < this.buffer.Length ) break;
342 string[] msg = this.message.Replace("\r","").Split('\n');
346 if ( this.message.IndexOf( "No such file or directory" ) != -1 )
347 msg = new string[]{};
351 if ( this.resultCode != 226 )
352 msg = new string[]{};
353 // throw new FtpException(result.Substring(4));
358 public bool DirectoryExists(string directory)
362 ChangeDir(directory);
373 /// Return the size of a file.
375 /// <param name="fileName"></param>
376 /// <returns></returns>
377 public long GetFileSize(string fileName)
379 if ( !this.loggedin ) this.Login();
381 this.sendCommand("SIZE " + fileName);
384 if ( this.resultCode == 213 )
385 size = long.Parse(this.result.Substring(4));
388 throw new FtpException(this.result.Substring(4));
395 /// Download a file to the Assembly's local directory,
396 /// keeping the same file name.
398 /// <param name="remFileName"></param>
399 public void Download(string remFileName)
401 this.Download(remFileName,"",false);
405 /// Download a remote file to the Assembly's local directory,
406 /// keeping the same file name, and set the resume flag.
408 /// <param name="remFileName"></param>
409 /// <param name="resume"></param>
410 public void Download(string remFileName,Boolean resume)
412 this.Download(remFileName,"",resume);
416 /// Download a remote file to a local file name which can include
417 /// a path. The local file name will be created or overwritten,
418 /// but the path must exist.
420 /// <param name="remFileName"></param>
421 /// <param name="locFileName"></param>
422 public void Download(string remFileName,string locFileName)
424 this.Download(remFileName,locFileName,false);
428 /// Download a remote file to a local file name which can include
429 /// a path, and set the resume flag. The local file name will be
430 /// created or overwritten, but the path must exist.
432 /// <param name="remFileName"></param>
433 /// <param name="locFileName"></param>
434 /// <param name="resume"></param>
435 public void Download(string remFileName,string locFileName,Boolean resume)
437 if ( !this.loggedin ) this.Login();
439 this.BinaryMode = true;
441 Debug.WriteLine("Downloading file " + remFileName + " from " + server + "/" + remotePath, "FtpClient" );
443 if (locFileName.Equals(""))
445 locFileName = remFileName;
448 FileStream output = null;
450 if ( !File.Exists(locFileName) )
451 output = File.Create(locFileName);
454 output = new FileStream(locFileName,FileMode.Open);
456 Socket cSocket = createDataSocket();
462 offset = output.Length;
466 this.sendCommand( "REST " + offset );
467 if ( this.resultCode != 350 )
469 //Server dosnt support resuming
471 Debug.WriteLine("Resuming not supported:" + result.Substring(4), "FtpClient" );
475 Debug.WriteLine("Resuming at offset " + offset, "FtpClient" );
476 output.Seek( offset, SeekOrigin.Begin );
481 this.sendCommand("RETR " + remFileName);
483 if ( this.resultCode != 150 && this.resultCode != 125 )
485 throw new FtpException(this.result.Substring(4));
488 DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
490 while ( timeout > DateTime.Now )
492 this.bytes = cSocket.Receive(buffer, buffer.Length, 0);
493 output.Write(this.buffer,0,this.bytes);
495 if ( this.bytes <= 0)
503 if ( cSocket.Connected ) cSocket.Close();
507 if( this.resultCode != 226 && this.resultCode != 250 )
508 throw new FtpException(this.result.Substring(4));
515 /// <param name="fileName"></param>
516 public void Upload(string fileName)
518 this.Upload(fileName,false);
523 /// Upload a file and set the resume flag.
525 /// <param name="fileName"></param>
526 /// <param name="resume"></param>
527 public void Upload(string fileName, bool resume)
529 if ( !this.loggedin ) this.Login();
531 Socket cSocket = null ;
538 this.BinaryMode = true;
540 offset = GetFileSize( Path.GetFileName(fileName) );
549 // open stream to read file
550 FileStream input = new FileStream(fileName,FileMode.Open);
552 if ( resume && input.Length < offset )
554 // different file size
555 Debug.WriteLine("Overwriting " + fileName, "FtpClient");
558 else if ( resume && input.Length == offset )
562 Debug.WriteLine("Skipping completed " + fileName + " - turn resume off to not detect.", "FtpClient");
566 // dont create untill we know that we need it
567 cSocket = this.createDataSocket();
571 this.sendCommand( "REST " + offset );
572 if ( this.resultCode != 350 )
574 Debug.WriteLine("Resuming not supported", "FtpClient");
579 this.sendCommand( "STOR " + Path.GetFileName(fileName) );
581 if ( this.resultCode != 125 && this.resultCode != 150 ) throw new FtpException(result.Substring(4));
585 Debug.WriteLine("Resuming at offset " + offset, "FtpClient" );
587 input.Seek(offset,SeekOrigin.Begin);
590 Debug.WriteLine( "Uploading file " + fileName + " to " + remotePath, "FtpClient" );
592 while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)
594 cSocket.Send(buffer, bytes, 0);
599 if (cSocket.Connected)
606 if( this.resultCode != 226 && this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));
610 /// Upload a directory and its file contents
612 /// <param name="path"></param>
613 /// <param name="recurse">Whether to recurse sub directories</param>
614 public void UploadDirectory(string path, bool recurse)
616 this.UploadDirectory(path,recurse,"*.*");
620 /// Upload a directory and its file contents
622 /// <param name="path"></param>
623 /// <param name="recurse">Whether to recurse sub directories</param>
624 /// <param name="mask">Only upload files of the given mask - everything is '*.*'</param>
625 public void UploadDirectory(string path, bool recurse, string mask)
627 string[] dirs = path.Replace("/",@"\").Split('\\');
628 string rootDir = dirs[ dirs.Length - 1 ];
630 // make the root dir if it doed not exist
631 if ( this.GetFileList(rootDir).Length < 1 ) this.MakeDir(rootDir);
633 this.ChangeDir(rootDir);
635 foreach ( string file in Directory.GetFiles(path,mask) )
637 this.Upload(file,true);
641 foreach ( string directory in Directory.GetDirectories(path) )
643 this.UploadDirectory(directory,recurse,mask);
647 this.ChangeDir("..");
651 /// Delete a file from the remote FTP server.
653 /// <param name="fileName"></param>
654 public void DeleteFile(string fileName)
656 if ( !this.loggedin ) this.Login();
658 this.sendCommand( "DELE " + fileName );
660 if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));
662 Debug.WriteLine( "Deleted file " + fileName, "FtpClient" );
666 /// Rename a file on the remote FTP server.
668 /// <param name="oldFileName"></param>
669 /// <param name="newFileName"></param>
670 /// <param name="overwrite">setting to false will throw exception if it exists</param>
671 public void RenameFile(string oldFileName,string newFileName, bool overwrite)
673 if ( !this.loggedin ) this.Login();
675 this.sendCommand( "RNFR " + oldFileName );
677 if ( this.resultCode != 350 ) throw new FtpException(this.result.Substring(4));
679 if ( !overwrite && this.GetFileList(newFileName).Length > 0 ) throw new FtpException("File already exists");
681 this.sendCommand( "RNTO " + newFileName );
683 if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));
685 Debug.WriteLine( "Renamed file " + oldFileName + " to " + newFileName, "FtpClient" );
689 /// Create a directory on the remote FTP server.
691 /// <param name="dirName"></param>
692 public void MakeDir(string dirName)
694 if ( !this.loggedin ) this.Login();
696 this.sendCommand( "MKD " + dirName );
698 if ( this.resultCode != 250 && this.resultCode != 257 ) throw new FtpException(this.result.Substring(4));
700 Debug.WriteLine( "Created directory " + dirName, "FtpClient" );
704 /// Delete a directory on the remote FTP server.
706 /// <param name="dirName"></param>
707 public void RemoveDir(string dirName)
709 if ( !this.loggedin ) this.Login();
711 this.sendCommand( "RMD " + dirName );
713 if ( this.resultCode != 250 ) throw new FtpException(this.result.Substring(4));
715 Debug.WriteLine( "Removed directory " + dirName, "FtpClient" );
719 /// Change the current working directory on the remote FTP server.
721 /// <param name="dirName"></param>
722 public void ChangeDir(string dirName)
724 if( dirName == null || dirName.Equals(".") || dirName.Length == 0 )
729 if ( !this.loggedin ) this.Login();
731 this.sendCommand( "CWD " + dirName );
733 if ( this.resultCode != 250 ) throw new FtpException(result.Substring(4));
735 this.sendCommand( "PWD" );
737 if ( this.resultCode != 257 ) throw new FtpException(result.Substring(4));
739 // gonna have to do better than this....
740 this.remotePath = this.message.Split('"')[1];
742 Debug.WriteLine( "Current directory is " + this.remotePath, "FtpClient" );
748 private void readResponse()
751 this.result = this.readLine();
753 if ( this.result.Length > 3 )
754 this.resultCode = int.Parse( this.result.Substring(0,3) );
762 /// <returns></returns>
763 private string readLine()
767 this.bytes = clientSocket.Receive( this.buffer, this.buffer.Length, 0 );
768 this.message += ASCII.GetString( this.buffer, 0, this.bytes );
770 if ( this.bytes < this.buffer.Length )
776 string[] msg = this.message.Split('\n');
778 if ( this.message.Length > 2 )
779 this.message = msg[ msg.Length - 2 ];
782 this.message = msg[0];
785 if ( this.message.Length > 4 && !this.message.Substring(3,1).Equals(" ") ) return this.readLine();
787 if ( this.verboseDebugging )
789 for(int i = 0; i < msg.Length - 1; i++)
791 Debug.Write( msg[i], "FtpClient" );
801 /// <param name="command"></param>
802 private void sendCommand(String command)
804 if ( this.verboseDebugging ) Debug.WriteLine(command,"FtpClient");
806 Byte[] cmdBytes = Encoding.ASCII.GetBytes( ( command + "\r\n" ).ToCharArray() );
807 clientSocket.Send( cmdBytes, cmdBytes.Length, 0);
812 /// when doing data transfers, we need to open another socket for it.
814 /// <returns>Connected socket</returns>
815 private Socket createDataSocket()
817 this.sendCommand("PASV");
819 if ( this.resultCode != 227 ) throw new FtpException(this.result.Substring(4));
821 int index1 = this.result.IndexOf('(');
822 int index2 = this.result.IndexOf(')');
824 string ipData = this.result.Substring(index1+1,index2-index1-1);
826 int[] parts = new int[6];
828 int len = ipData.Length;
832 for (int i = 0; i < len && partCount <= 6; i++)
834 char ch = char.Parse( ipData.Substring(i,1) );
836 if ( char.IsDigit(ch) )
840 throw new FtpException("Malformed PASV result: " + result);
842 if ( ch == ',' || i+1 == len )
846 parts[partCount++] = int.Parse(buf);
851 throw new FtpException("Malformed PASV result (not supported?): " + this.result, ex);
856 string ipAddress = parts[0] + "."+ parts[1]+ "." + parts[2] + "." + parts[3];
858 int port = (parts[4] << 8) + parts[5];
860 Socket socket = null;
861 IPEndPoint ep = null;
865 socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
866 ep = new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);
872 if ( socket != null && socket.Connected ) socket.Close();
874 throw new FtpException("Can't connect to remote server", ex);
881 /// Always release those sockets.
883 private void cleanup()
885 if ( this.clientSocket!=null )
887 this.clientSocket.Close();
888 this.clientSocket = null;
890 this.loggedin = false;
902 /**************************************************************************************************************/
903 #region Async methods (auto generated)
906 WinInetApi.FtpClient ftp = new WinInetApi.FtpClient();
908 MethodInfo[] methods = ftp.GetType().GetMethods(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public);
910 foreach ( MethodInfo method in methods )
914 foreach ( ParameterInfo i in method.GetParameters() )
916 param += i.ParameterType.Name + " " + i.Name + ",";
917 values += i.Name + ",";
921 Debug.WriteLine("private delegate " + method.ReturnType.Name + " " + method.Name + "Callback(" + param.TrimEnd(',') + ");");
923 Debug.WriteLine("public System.IAsyncResult Begin" + method.Name + "( " + param + " System.AsyncCallback callback )");
924 Debug.WriteLine("{");
925 Debug.WriteLine("" + method.Name + "Callback ftpCallback = new " + method.Name + "Callback(" + values + " this." + method.Name + ");");
926 Debug.WriteLine("return ftpCallback.BeginInvoke(callback, null);");
927 Debug.WriteLine("}");
928 Debug.WriteLine("public void End" + method.Name + "(System.IAsyncResult asyncResult)");
929 Debug.WriteLine("{");
930 Debug.WriteLine(method.Name + "Callback fc = (" + method.Name + "Callback) ((AsyncResult)asyncResult).AsyncDelegate;");
931 Debug.WriteLine("fc.EndInvoke(asyncResult);");
932 Debug.WriteLine("}");
933 //Debug.WriteLine(method);
938 private delegate void LoginCallback();
939 public System.IAsyncResult BeginLogin( System.AsyncCallback callback )
941 LoginCallback ftpCallback = new LoginCallback( this.Login);
942 return ftpCallback.BeginInvoke(callback, null);
944 private delegate void CloseCallback();
945 public System.IAsyncResult BeginClose( System.AsyncCallback callback )
947 CloseCallback ftpCallback = new CloseCallback( this.Close);
948 return ftpCallback.BeginInvoke(callback, null);
950 private delegate String[] GetFileListCallback();
951 public System.IAsyncResult BeginGetFileList( System.AsyncCallback callback )
953 GetFileListCallback ftpCallback = new GetFileListCallback( this.GetFileList);
954 return ftpCallback.BeginInvoke(callback, null);
956 private delegate String[] GetFileListMaskCallback(String mask);
957 public System.IAsyncResult BeginGetFileList( String mask, System.AsyncCallback callback )
959 GetFileListMaskCallback ftpCallback = new GetFileListMaskCallback(this.GetFileList);
960 return ftpCallback.BeginInvoke(mask, callback, null);
962 private delegate Int64 GetFileSizeCallback(String fileName);
963 public System.IAsyncResult BeginGetFileSize( String fileName, System.AsyncCallback callback )
965 GetFileSizeCallback ftpCallback = new GetFileSizeCallback(this.GetFileSize);
966 return ftpCallback.BeginInvoke(fileName, callback, null);
968 private delegate void DownloadCallback(String remFileName);
969 public System.IAsyncResult BeginDownload( String remFileName, System.AsyncCallback callback )
971 DownloadCallback ftpCallback = new DownloadCallback(this.Download);
972 return ftpCallback.BeginInvoke(remFileName, callback, null);
974 private delegate void DownloadFileNameResumeCallback(String remFileName,Boolean resume);
975 public System.IAsyncResult BeginDownload( String remFileName,Boolean resume, System.AsyncCallback callback )
977 DownloadFileNameResumeCallback ftpCallback = new DownloadFileNameResumeCallback(this.Download);
978 return ftpCallback.BeginInvoke(remFileName, resume, callback, null);
980 private delegate void DownloadFileNameFileNameCallback(String remFileName,String locFileName);
981 public System.IAsyncResult BeginDownload( String remFileName,String locFileName, System.AsyncCallback callback )
983 DownloadFileNameFileNameCallback ftpCallback = new DownloadFileNameFileNameCallback(this.Download);
984 return ftpCallback.BeginInvoke(remFileName, locFileName, callback, null);
986 private delegate void DownloadFileNameFileNameResumeCallback(String remFileName,String locFileName,Boolean resume);
987 public System.IAsyncResult BeginDownload( String remFileName,String locFileName,Boolean resume, System.AsyncCallback callback )
989 DownloadFileNameFileNameResumeCallback ftpCallback = new DownloadFileNameFileNameResumeCallback(this.Download);
990 return ftpCallback.BeginInvoke(remFileName, locFileName, resume, callback, null);
992 private delegate void UploadCallback(String fileName);
993 public System.IAsyncResult BeginUpload( String fileName, System.AsyncCallback callback )
995 UploadCallback ftpCallback = new UploadCallback(this.Upload);
996 return ftpCallback.BeginInvoke(fileName, callback, null);
998 private delegate void UploadFileNameResumeCallback(String fileName,Boolean resume);
999 public System.IAsyncResult BeginUpload( String fileName,Boolean resume, System.AsyncCallback callback )
1001 UploadFileNameResumeCallback ftpCallback = new UploadFileNameResumeCallback(this.Upload);
1002 return ftpCallback.BeginInvoke(fileName, resume, callback, null);
1004 private delegate void UploadDirectoryCallback(String path,Boolean recurse);
1005 public System.IAsyncResult BeginUploadDirectory( String path,Boolean recurse, System.AsyncCallback callback )
1007 UploadDirectoryCallback ftpCallback = new UploadDirectoryCallback(this.UploadDirectory);
1008 return ftpCallback.BeginInvoke(path, recurse, callback, null);
1010 private delegate void UploadDirectoryPathRecurseMaskCallback(String path,Boolean recurse,String mask);
1011 public System.IAsyncResult BeginUploadDirectory( String path,Boolean recurse,String mask, System.AsyncCallback callback )
1013 UploadDirectoryPathRecurseMaskCallback ftpCallback = new UploadDirectoryPathRecurseMaskCallback(this.UploadDirectory);
1014 return ftpCallback.BeginInvoke(path, recurse, mask, callback, null);
1016 private delegate void DeleteFileCallback(String fileName);
1017 public System.IAsyncResult BeginDeleteFile( String fileName, System.AsyncCallback callback )
1019 DeleteFileCallback ftpCallback = new DeleteFileCallback(this.DeleteFile);
1020 return ftpCallback.BeginInvoke(fileName, callback, null);
1022 private delegate void RenameFileCallback(String oldFileName,String newFileName,Boolean overwrite);
1023 public System.IAsyncResult BeginRenameFile( String oldFileName,String newFileName,Boolean overwrite, System.AsyncCallback callback )
1025 RenameFileCallback ftpCallback = new RenameFileCallback(this.RenameFile);
1026 return ftpCallback.BeginInvoke(oldFileName, newFileName, overwrite, callback, null);
1028 private delegate void MakeDirCallback(String dirName);
1029 public System.IAsyncResult BeginMakeDir( String dirName, System.AsyncCallback callback )
1031 MakeDirCallback ftpCallback = new MakeDirCallback(this.MakeDir);
1032 return ftpCallback.BeginInvoke(dirName, callback, null);
1034 private delegate void RemoveDirCallback(String dirName);
1035 public System.IAsyncResult BeginRemoveDir( String dirName, System.AsyncCallback callback )
1037 RemoveDirCallback ftpCallback = new RemoveDirCallback(this.RemoveDir);
1038 return ftpCallback.BeginInvoke(dirName, callback, null);
1040 private delegate void ChangeDirCallback(String dirName);
1041 public System.IAsyncResult BeginChangeDir( String dirName, System.AsyncCallback callback )
1043 ChangeDirCallback ftpCallback = new ChangeDirCallback(this.ChangeDir);
1044 return ftpCallback.BeginInvoke(dirName, callback, null);