我正在尝试在我的 FTP 服务器上创建一个目录。谷歌搜索在这里向我展示了这个问题。 所以我按照乔恩写的作为他的答案,并且有:
private static void MakeDirectory(string directory)
{
Log("Making directory...");
var request = (FtpWebRequest)WebRequest.Create(directory);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("user", "pass");
try
{
using (var resp = (FtpWebResponse)request.GetResponse()) // Exception occurs here
{
Log(resp.StatusCode.ToString());
}
}
catch (WebException ex)
{
Log(ex.Message);
}
}
我有一种方法来验证目录是否存在:
public bool DirectoryExists(string directory)
{
bool directoryExists;
var request = (FtpWebRequest)WebRequest.Create(directory);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("user", "pass");
try
{
using (request.GetResponse())
{
directoryExists = true;
}
}
catch (WebException)
{
directoryExists = false;
}
return directoryExists;
}
检查确实有效。如果存在,则返回true
,如果不存在,则返回false
。但是,每当我运行 my 时MakeDirectory()
,都会在上述行中出现异常:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
我错过了什么吗?为什么GetResponse()
我的目录检查工作,而不是我的工作MakeDirectory()
?