Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Catch FTP Errors with No Abort

Discussion in 'Scripting' started by patmaric, May 5, 2020.

  1. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    I want to get a file from an FTP server, but when there is no Internet connection I want to get the file locally. When I force an FTP error, the C# program aborts. How can I "catch" this error? When I debug it, it goes to other c# scripts, such as ExecuteEvents.cs, and somewhere along the way it aborts. It never comes back to my script. When it gets to line:

    string fileData = request.DownloadString(ftpPath);

    it goes off into pre-packaged scripts and aborts. It works fine when I don't force the FTP error.

    _____________________________________________________________
    string fileToFTP = "myFile.txt";
    string fileData = GetFTPFile.FtpFile(FileToFTP);
    _____________________________________________________________

    public class GetFtpFile : MonoBehaviour
    {
    public static string[] FtpFile(string fileToFTP)
    {
    string ftpPath = (@"ftp://myftphost.com/" + fileToFTP);
    WebClient request = new WebClient
    {
    Credentials = new NetworkCredential("username", "password")
    };
    string fileData = request.DownloadString(ftpPath);

    return fileData;
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
  3. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    Thanks! I thought that wouldn't work because it was aborting before it came back to my script, but it does! Wonderful.