Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Security.Permissions;
  5. using System.Security.Authentication;
  6. using System.Security.AccessControl;
  7.  
  8. namespace GetXMLNodes
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. try
  15. {
  16. ftp ftpClient = new ftp(@"ftp://...ip", "username", "password");
  17. ftpClient.download(@"3636594381842_2015-04-08T12_08_27.177Z Status.xml", @"C:UsersHIKMETDesktopProje_XML_to_SCADADownloadedXML");
  18. ftpClient = null;
  19. Console.WriteLine("download success..!");
  20. Console.ReadLine();
  21. }
  22.  
  23. catch(Exception ex)
  24. {
  25. Console.WriteLine(ex.Message);
  26. }
  27. }
  28. }
  29.  
  30.  
  31.  
  32. class ftp
  33. {
  34. private string host = null;
  35. private string user = null;
  36. private string pass = null;
  37. private FtpWebRequest ftpRequest = null;
  38. private FtpWebResponse ftpResponse = null;
  39. private Stream ftpStream = null;
  40. private int bufferSize = 2048;
  41.  
  42. /* Construct Object */
  43. public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
  44.  
  45. /* Download File */
  46. public void download(string remoteFile, string localFile)
  47. {
  48. try
  49. {
  50. /* Create an FTP Request */
  51. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  52. /* Log in to the FTP Server with the User Name and Password Provided */
  53. ftpRequest.Credentials = new NetworkCredential(user, pass);
  54. /* When in doubt, use these options */
  55. ftpRequest.UseBinary = true;
  56. ftpRequest.UsePassive = true;
  57. ftpRequest.KeepAlive = true;
  58. /* Specify the Type of FTP Request */
  59. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  60. /* Establish Return Communication with the FTP Server */
  61. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  62. /* Get the FTP Server's Response Stream */
  63. ftpStream = ftpResponse.GetResponseStream();
  64. /* Open a File Stream to Write the Downloaded File */
  65. //FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write,localFile);
  66. //f.AllLocalFiles = FileIOPermissionAccess.Write;
  67. //f.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, localFile);
  68. //f.Demand();
  69. FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  70. /* Buffer for the Downloaded Data */
  71. byte[] byteBuffer = new byte[bufferSize];
  72. int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  73. /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  74. try
  75. {
  76. while (bytesRead > 0)
  77. {
  78. localFileStream.Write(byteBuffer, 0, bytesRead);
  79. bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  80. }
  81. }
  82. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  83. /* Resource Cleanup */
  84. localFileStream.Close();
  85. ftpStream.Close();
  86. ftpResponse.Close();
  87. ftpRequest = null;
  88. }
  89. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  90. return;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement