Advertisement
Guest User

Woo

a guest
Mar 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Net;
  3. using System.IO;
  4. using System.Collections;
  5.  
  6. namespace System.Net {
  7. public class ftp : ScriptableObject{
  8.     public string host = null;
  9.     public string user = null;
  10.     public string pass = null;
  11.  
  12.     public bool netSaving = false;
  13.     private FtpWebRequest ftpRequest = null;
  14.     private FtpWebResponse ftpResponse = null;
  15.     private Stream ftpStream = null;
  16.     private int bufferSize = 2048;
  17.        
  18.     /* Construct Object */
  19.     public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
  20.    
  21.     void Update() {
  22.             if(netSaving) {
  23.             NetSave();
  24.             netSaving = false;
  25.         }
  26.     }
  27.  
  28.     /* Download File */
  29.     public void download(string remoteFile, string localFile)
  30.     {
  31.         try
  32.         {
  33.             /* Create an FTP Request */
  34.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  35.             /* Log in to the FTP Server with the User Name and Password Provided */
  36.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  37.             /* When in doubt, use these options */
  38.             ftpRequest.UseBinary = true;
  39.             ftpRequest.UsePassive = true;
  40.             ftpRequest.KeepAlive = true;
  41.             /* Specify the Type of FTP Request */
  42.             ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  43.             /* Establish Return Communication with the FTP Server */
  44.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  45.             /* Get the FTP Server's Response Stream */
  46.             ftpStream = ftpResponse.GetResponseStream();
  47.             /* Open a File Stream to Write the Downloaded File */
  48.             FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  49.             /* Buffer for the Downloaded Data */
  50.             byte[] byteBuffer = new byte[bufferSize];
  51.             int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  52.             /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  53.             try
  54.             {
  55.                 while (bytesRead > 0)
  56.                 {
  57.                     localFileStream.Write(byteBuffer, 0, bytesRead);
  58.                     bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  59.                 }
  60.             }
  61.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  62.             /* Resource Cleanup */
  63.             localFileStream.Close();
  64.             ftpStream.Close();
  65.             ftpResponse.Close();
  66.             ftpRequest = null;
  67.         }
  68.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  69.         return;
  70.     }
  71.  
  72.     /* Upload File */
  73.     public void upload(string remoteFile, string localFile)
  74.     {
  75.         try
  76.         {
  77.             /* Create an FTP Request */
  78.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  79.             /* Log in to the FTP Server with the User Name and Password Provided */
  80.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  81.             /* When in doubt, use these options */
  82.             ftpRequest.UseBinary = true;
  83.             ftpRequest.UsePassive = true;
  84.             ftpRequest.KeepAlive = true;
  85.             /* Specify the Type of FTP Request */
  86.             ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  87.             /* Establish Return Communication with the FTP Server */
  88.             ftpStream = ftpRequest.GetRequestStream();
  89.             /* Open a File Stream to Read the File for Upload */
  90.             FileStream localFileStream = File.OpenRead (localFile);
  91.             /* Buffer for the Downloaded Data */
  92.             byte[] byteBuffer = new byte[bufferSize];
  93.             int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  94.             /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
  95.             try
  96.             {
  97.                 while (bytesSent != 0)
  98.                 {
  99.                     ftpStream.Write(byteBuffer, 0, bytesSent);
  100.                     bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  101.                 }
  102.             }
  103.             catch (Exception ex) { Debug.Log(ex.ToString()); }
  104.             /* Resource Cleanup */
  105.             localFileStream.Close();
  106.             ftpStream.Close();
  107.             ftpRequest = null;
  108.         }
  109.         catch (Exception ex) { Debug.Log(ex.ToString()); }
  110.         return;
  111.     }
  112.  
  113.     /* Delete File */
  114.     public void delete(string deleteFile)
  115.     {
  116.         try
  117.         {
  118.             /* Create an FTP Request */
  119.             ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
  120.             /* Log in to the FTP Server with the User Name and Password Provided */
  121.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  122.             /* When in doubt, use these options */
  123.             ftpRequest.UseBinary = true;
  124.             ftpRequest.UsePassive = true;
  125.             ftpRequest.KeepAlive = true;
  126.             /* Specify the Type of FTP Request */
  127.             ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  128.             /* Establish Return Communication with the FTP Server */
  129.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  130.             /* Resource Cleanup */
  131.             ftpResponse.Close();
  132.             ftpRequest = null;
  133.         }
  134.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  135.         return;
  136.     }
  137.  
  138.     /* Rename File */
  139.     public void rename(string currentFileNameAndPath, string newFileName)
  140.     {
  141.         try
  142.         {
  143.             /* Create an FTP Request */
  144.             ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
  145.             /* Log in to the FTP Server with the User Name and Password Provided */
  146.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  147.             /* When in doubt, use these options */
  148.             ftpRequest.UseBinary = true;
  149.             ftpRequest.UsePassive = true;
  150.             ftpRequest.KeepAlive = true;
  151.             /* Specify the Type of FTP Request */
  152.             ftpRequest.Method = WebRequestMethods.Ftp.Rename;
  153.             /* Rename the File */
  154.             ftpRequest.RenameTo = newFileName;
  155.             /* Establish Return Communication with the FTP Server */
  156.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  157.             /* Resource Cleanup */
  158.             ftpResponse.Close();
  159.             ftpRequest = null;
  160.         }
  161.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  162.         return;
  163.     }
  164.  
  165.     /* Create a New Directory on the FTP Server */
  166.     public void createDirectory(string newDirectory)
  167.     {
  168.         try
  169.         {
  170.             /* Create an FTP Request */
  171.             ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
  172.             /* Log in to the FTP Server with the User Name and Password Provided */
  173.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  174.             /* When in doubt, use these options */
  175.             ftpRequest.UseBinary = true;
  176.             ftpRequest.UsePassive = true;
  177.             ftpRequest.KeepAlive = true;
  178.             /* Specify the Type of FTP Request */
  179.             ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
  180.             /* Establish Return Communication with the FTP Server */
  181.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  182.             /* Resource Cleanup */
  183.             ftpResponse.Close();
  184.             ftpRequest = null;
  185.         }
  186.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  187.         return;
  188.     }
  189.  
  190.     /* Get the Date/Time a File was Created */
  191.     public string getFileCreatedDateTime(string fileName)
  192.     {
  193.         try
  194.         {
  195.             /* Create an FTP Request */
  196.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  197.             /* Log in to the FTP Server with the User Name and Password Provided */
  198.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  199.             /* When in doubt, use these options */
  200.             ftpRequest.UseBinary = true;
  201.             ftpRequest.UsePassive = true;
  202.             ftpRequest.KeepAlive = true;
  203.             /* Specify the Type of FTP Request */
  204.             ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  205.             /* Establish Return Communication with the FTP Server */
  206.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  207.             /* Establish Return Communication with the FTP Server */
  208.             ftpStream = ftpResponse.GetResponseStream();
  209.             /* Get the FTP Server's Response Stream */
  210.             StreamReader ftpReader = new StreamReader(ftpStream);
  211.             /* Store the Raw Response */
  212.             string fileInfo = null;
  213.             /* Read the Full Response Stream */
  214.             try { fileInfo = ftpReader.ReadToEnd(); }
  215.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  216.             /* Resource Cleanup */
  217.             ftpReader.Close();
  218.             ftpStream.Close();
  219.             ftpResponse.Close();
  220.             ftpRequest = null;
  221.             /* Return File Created Date Time */
  222.             return fileInfo;
  223.         }
  224.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  225.         /* Return an Empty string Array if an Exception Occurs */
  226.         return "";
  227.     }
  228.  
  229.     /* Get the Size of a File */
  230.     public string getFileSize(string fileName)
  231.     {
  232.         try
  233.         {
  234.             /* Create an FTP Request */
  235.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  236.             /* Log in to the FTP Server with the User Name and Password Provided */
  237.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  238.             /* When in doubt, use these options */
  239.             ftpRequest.UseBinary = true;
  240.             ftpRequest.UsePassive = true;
  241.             ftpRequest.KeepAlive = true;
  242.             /* Specify the Type of FTP Request */
  243.             ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
  244.             /* Establish Return Communication with the FTP Server */
  245.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  246.             /* Establish Return Communication with the FTP Server */
  247.             ftpStream = ftpResponse.GetResponseStream();
  248.             /* Get the FTP Server's Response Stream */
  249.             StreamReader ftpReader = new StreamReader(ftpStream);
  250.             /* Store the Raw Response */
  251.             string fileInfo = null;
  252.             /* Read the Full Response Stream */
  253.             try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
  254.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  255.             /* Resource Cleanup */
  256.             ftpReader.Close();
  257.             ftpStream.Close();
  258.             ftpResponse.Close();
  259.             ftpRequest = null;
  260.             /* Return File Size */
  261.             return fileInfo;
  262.         }
  263.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  264.         /* Return an Empty string Array if an Exception Occurs */
  265.         return "";
  266.     }
  267.  
  268.     /* List Directory Contents File/Folder Name Only */
  269.     public string[] directoryListSimple(string directory)
  270.     {
  271.         try
  272.         {
  273.             /* Create an FTP Request */
  274.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(@host + "/" + directory);
  275.             /* Log in to the FTP Server with the User Name and Password Provided */
  276.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  277.             /* When in doubt, use these options */
  278.             ftpRequest.UseBinary = true;
  279.             ftpRequest.UsePassive = true;
  280.             ftpRequest.KeepAlive = true;
  281.             /* Specify the Type of FTP Request */
  282.             ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  283.             /* Establish Return Communication with the FTP Server */
  284.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  285.             /* Establish Return Communication with the FTP Server */
  286.             ftpStream = ftpResponse.GetResponseStream();
  287.             /* Get the FTP Server's Response Stream */
  288.             StreamReader ftpReader = new StreamReader(ftpStream);
  289.             /* Store the Raw Response */
  290.             string directoryRaw = null;
  291.             /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  292.             try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
  293.             catch (Exception ex) { Debug.Log(ex.ToString()); }
  294.             /* Resource Cleanup */
  295.             ftpReader.Close();
  296.             ftpStream.Close();
  297.             ftpResponse.Close();
  298.             ftpRequest = null;
  299.             /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  300.             try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
  301.             catch (Exception ex) { Debug.Log(ex.ToString()); }
  302.         }
  303.         catch (Exception ex) { Debug.Log(ex.ToString()); }
  304.         /* Return an Empty string Array if an Exception Occurs */
  305.         return new string[] { "" };
  306.     }
  307.  
  308.     /* List Directory Contents in Detail (Name, Size, Created, etc.) */
  309.     public string[] directoryListDetailed(string directory)
  310.     {
  311.         try
  312.         {
  313.             /* Create an FTP Request */
  314.             ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
  315.             /* Log in to the FTP Server with the User Name and Password Provided */
  316.             ftpRequest.Credentials = new NetworkCredential(user, pass);
  317.             /* When in doubt, use these options */
  318.             ftpRequest.UseBinary = true;
  319.             ftpRequest.UsePassive = true;
  320.             ftpRequest.KeepAlive = true;
  321.             /* Specify the Type of FTP Request */
  322.             ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  323.             /* Establish Return Communication with the FTP Server */
  324.             ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  325.             /* Establish Return Communication with the FTP Server */
  326.             ftpStream = ftpResponse.GetResponseStream();
  327.             /* Get the FTP Server's Response Stream */
  328.             StreamReader ftpReader = new StreamReader(ftpStream);
  329.             /* Store the Raw Response */
  330.             string directoryRaw = null;
  331.             /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  332.             try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
  333.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  334.             /* Resource Cleanup */
  335.             ftpReader.Close();
  336.             ftpStream.Close();
  337.             ftpResponse.Close();
  338.             ftpRequest = null;
  339.             /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  340.             try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
  341.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  342.         }
  343.         catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  344.         /* Return an Empty string Array if an Exception Occurs */
  345.         return new string[] { "" };
  346.     }
  347.  
  348.     /////////////////////////////////////////
  349.     void NetSave() {
  350.         ftp ftpClient = new ftp ("ftp://185.27.134.11/", "b17_17716291", "Deusche1");
  351.         ftpClient.upload("htdocs/Test.txt", @"C:\Users\stonedmaster\Desktop\Game Creation Assets\Flux Game Stuff\Flux Online\Dropbox\Flux Online\Assets\Test.txt");
  352.     }
  353.  }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement