Search code examples
c#ftpwebrequest

Invalid URI: The format of the URI cannot be determined


I am trying to create a folder and upload files via FTP, but I get the following error:

Invalid URI: The format of the URI cannot be determined

I look for the web solution but nothing works so far

<add key="FTP" value="ftp://192.168.1.1:21" />


string ftpURI = ConfigurationManager.AppSettings["FTP"];

string nombre = (ftpURI);

WebRequest ftpRequest = WebRequest.Create(nombre + @"//Images//test");
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Credentials = new NetworkCredential("rmorquecho@xxx.com", "12345*");
using (var resp = (FtpWebResponse)ftpRequest.GetResponse())
                    {
                        Console.WriteLine(resp.StatusCode);
                    }

I don't know what I'm doing wrong, I intend to verify if the folder exists and if I don't create it


Solution

  • ConfigurationManager.AppSettings["FTP"] is an invalid URI. You need to fix that.

    You probably need to add ftp to the url in the app settings. Like this "ftp://192.168.1.1:21"

    In the end call GetResponse

    using (var resp = (FtpWebResponse) ftpRequest.GetResponse()){
            Console.WriteLine(resp.StatusCode);
    }
    

    nombre + @"//Images//test" does not create a valid url. You are using double /

    Also make sure directory Images exists.