I have added the following code in my C#.net application in visual studio 2010
WebRequest request = WebRequest.Create("ftp://myftp.com");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
But I am getting following error.
The requested URI is invalid for this FTP command.
Please suggest solution. Thanks
If you just want to check file information you should try changing the request.Method
to WebRequestMethods.Ftp.ListDirectoryDetails
instead:
WebRequest request = WebRequest.Create("ftp://myftp.com");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
Uri should be ftp:// + IP or ftpServerName + / Folder_to_create_Name
So, see follow code:
WebRequest request = WebRequest.Create("ftp://myftp.com/FOLDER_TO_CREATE/");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("myusername", "12344");
using (var resp = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
You must specify a sub-directory you wish to create; you can't create the root, which is what it appears you are trying to do. Hence the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With