Sprache: C#
Order rekursiv bei FTP erstellen
Man ruft die Methode CreateDirectory auf die Methode _CreateDirectory ist nur eine Hilfsmethode.
Beispielausruf:
CreateDirectory("Test1/Test2/Test3", @"ftp://bla.bla.de", "blaUser", "blaPasswort");
private void _CreateDirectory(String host, String path, String CurrentPath, String username, String password)
{
try
{
WebRequest request = WebRequest.Create(host + "/" + CurrentPath);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
request.Proxy = null;
WebResponse response = request.GetResponse();
}
catch
{
// Guess something should happen here
}
int pos = path.IndexOf("/");
if (pos != path.Length - 1)
{
path = path.Substring(pos + 1, path.Length - (pos + 1));
int ipos = path.IndexOf("/");
CurrentPath += "/" + path.Substring(0, ipos);
_CreateDirectory(host, path, CurrentPath, username, password);
}
}
public void CreateDirectory(String path, String host, String username, String password)
{
if (!(path.ToCharArray()[path.Length - 1] == '/'))
path += "/";
int pos = path.IndexOf("/");
String folder = path.Substring(0, pos);
_CreateDirectory(host, path, folder, username, password);
}
private void _CreateDirectory(String host, String path, String CurrentPath, String username, String password)
{
try
{
WebRequest request = WebRequest.Create(host + "/" + CurrentPath);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
request.Proxy = null;
WebResponse response = request.GetResponse();
}
catch
{
// Guess something should happen here
}
int pos = path.IndexOf("/");
if (pos != path.Length - 1)
{
path = path.Substring(pos + 1, path.Length - (pos + 1));
int ipos = path.IndexOf("/");
CurrentPath += "/" + path.Substring(0, ipos);
_CreateDirectory(host, path, CurrentPath, username, password);
}
}
public void CreateDirectory(String path, String host, String username, String password)
{
if (!(path.ToCharArray()[path.Length - 1] == '/'))
path += "/";
int pos = path.IndexOf("/");
String folder = path.Substring(0, pos);
_CreateDirectory(host, path, folder, username, password);
}
Alte URL:
/snippet/ftp-ordner-rekursiv-erstellen/1368