请求的URI对此FTP命令无效

时间:2011-04-27 10:19:26

标签: c# .net

我在Visual Studio 2010的C#.net应用程序中添加了以下代码

 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);
            }

但我收到了以下错误。

The requested URI is invalid for this FTP command.

请建议解决方案。感谢

3 个答案:

答案 0 :(得分:16)

如果您只想查看文件信息,请尝试将request.Method更改为WebRequestMethods.Ftp.ListDirectoryDetails

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);
        }

答案 1 :(得分:10)

Uri应该是ftp:// + IP或ftpServerName + / Folder_to_create_Name

所以,请参阅以下代码:

 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);
 }

答案 2 :(得分:2)

您必须指定要创建的子目录;你不能创建根,这是你想要做的。因此错误。