How to Create a Directory on Ftp Server Using C#

How do I create a directory on FTP server using C#?

Use FtpWebRequest, with a method of 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);
}
}
}

FTP create folder and upload files in C#

I managed to get it working with:

private void FtpCreateFolder(string ftpAddress, string ftpUName, string ftpPWord)
{
WebRequest ftpRequest = WebRequest.Create("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER");
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
}

I guess the problem was using FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(...). Thanks anyway, SO, hope someone else finds this useful!

Creating a directory on FTP server

As it turns out, this was an issue on the part of the company who runs our web servers. They did some FTP update last night that affected not only myself, but all their clients. They have since fixed the problem. It just took them a day to admit that it was their fault.

Create folder on ftp

Well, "I want to get exception if folder already exists" and "how to make it overwrite the existing folder" are two opposing questions.

At any rate, I just implemented code to do this the other day. Just check if the directory already exists first. And then respond based on that. There's no point in trying to create a directory that already exists.

And if you need to overwrite it somehow, then delete the existing directory before creating the new one.

You can see the code I wrote for this in the article An FtpClient Class and WinForm Control, although it will just overwrite existing content.

Creating Sub Folders in FTP Folder in C# Windows Applications

In your method declaration you are indicating that the parameter dictionary is of type string. When you want to call your method inside your button1_click event, you just have to pass any string but without using the term string before it again.

Example:

private void button1_click(object sender, EventArgs e)
{
string directory = @"\Path\You\Want\To\Pass";
DateTime iDate;
iDate = dateTimePicker1.Value;
CreateDirectoryFTP(directory, iDate); // You directly pass the variable to the method
}

Or you could pass it directly if you don't want to use a variable to pass the path:

private void button1_click(object sender, EventArgs e)
{
DateTime iDate;
iDate = dateTimePicker1.Value;
CreateDirectoryFTP(@"\Path\You\Want\To\Pass", iDate);
}

To create subfolders, pass the date to the method CreateDirectoryFTP:

private void CreateDirectoryFTP(string directory, DateTime date)
{
string path = @"/" + directory;
WebRequest request = WebRequest.Create(FtpHost + path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
try
{
request.GetResponse();
}
catch (Exception e)
{
//directory exists
}

string pathToDay = path + "/" + date.Day.ToString();
// And now you can create the subfolder like you did it for the main folder

string pathToMonth = path + "/" + date.Month.ToString();
// Also with the month and the year you can do it like you did it for the main folder
}


Related Topics



Leave a reply



Submit