如何使用 C# 在 FTP 服务器上创建目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/860638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a directory on FTP server using C#?
提问by Anthony Brien
What's an easy way to create a directory on an FTP server using C#?
使用 C# 在 FTP 服务器上创建目录的简单方法是什么?
I figured out how to upload a file to an already existing folder like this:
我想出了如何将文件上传到已经存在的文件夹,如下所示:
using (WebClient webClient = new WebClient())
{
string filePath = "d:/users/abrien/file.txt";
webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath);
}
However, if I want to upload to users/abrien
, I get a WebException
saying the file is unavailable. I assume this is because I need to create the new folder before uploading my file, but WebClient
doesn't seem to have any methods to accomplish that.
但是,如果我想上传到users/abrien
,我会收到一个WebException
说该文件不可用的消息。我认为这是因为我需要在上传文件之前创建新文件夹,但WebClient
似乎没有任何方法可以实现。
采纳答案by Jon Skeet
Use FtpWebRequest
, with a method of WebRequestMethods.Ftp.MakeDirectory
.
使用FtpWebRequest
,方法为WebRequestMethods.Ftp.MakeDirectory
。
For example:
例如:
using System;
using System.Net;
class Test
{
static void Main()
{
WebRequest request = WebRequest.Create("ftp://host.com/directory");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("user", "pass");
using (var resp = (FtpWebResponse) request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
}
}
回答by Fredrik M?rk
Something like this:
像这样的东西:
// remoteUri points out an ftp address ("ftp://server/thefoldertocreate")
WebRequest request = WebRequest.Create(remoteUri);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
WebResponse response = request.GetResponse();
(a bit late. how odd.)
(有点晚了。真奇怪。)
回答by Yannick Richard
Here is the answer if you want to create nested directories
如果要创建嵌套目录,这是答案
There is no clean way to check if a folder exist on the ftp so you have to loop and create all the nested structure one folder at the time
没有干净的方法来检查 ftp 上是否存在文件夹,因此您必须循环并在一个文件夹中创建所有嵌套结构
public static void MakeFTPDir(string ftpAddress, string pathToCreate, string login, string password, byte[] fileContents, string ftpProxy = null)
{
FtpWebRequest reqFTP = null;
Stream ftpStream = null;
string[] subDirs = pathToCreate.Split('/');
string currentDir = string.Format("ftp://{0}", ftpAddress);
foreach (string subDir in subDirs)
{
try
{
currentDir = currentDir + "/" + subDir;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(login, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
//directory already exist I know that is weak but there is no way to check if a folder exist on ftp...
}
}
}
回答by Adi Sembiring
Creating an FTP directory might be complicated since you have to check if the destination folder exists or not. You may need to use an FTP library to check and create a directory. You can take a look at this one: http://www.componentpro.com/ftp.net/and this example: http://www.componentpro.com/doc/ftp/Creating-a-new-directory-Synchronously.htm
创建 FTP 目录可能很复杂,因为您必须检查目标文件夹是否存在。您可能需要使用 FTP 库来检查和创建目录。你可以看看这个:http: //www.componentpro.com/ftp.net/和这个例子:http: //www.componentpro.com/doc/ftp/Creating-a-new-directory-Synchronously .htm