Hello all!

Ok, I'm writing an app that creates a specific folder tree on my FTP server. The app asks for a customer name and a project ID. When that is given and button is clicked, I want to create a folder name based off of the customer name, a subfolder under that with the Project ID number, and then 3 specific subfolders under the ProjectID folder. Here's my code:

FtpWebRequest req = (FtpWebRequest)WebRequest.Create(custPath);             //---------------------
req.Credentials = new NetworkCredential("username", "passw");               // Create ftp folder for 
req.Method = WebRequestMethods.Ftp.MakeDirectory;                           // customer name and project ID
FtpWebResponse makeDirectoryResponse = (FtpWebResponse)req.GetResponse();   //------------------------------

FtpWebRequest req1 = (FtpWebRequest)WebRequest.Create(custPath1);           //--------------------
req1.Credentials = new NetworkCredential("username", "passw");              // Creates XML folder
req1.Method = WebRequestMethods.Ftp.MakeDirectory;                          //
FtpWebResponse makeDirectoryResponse1 = (FtpWebResponse)req1.GetResponse(); //--------------------

FtpWebRequest req2 = (FtpWebRequest)WebRequest.Create(custPath2);           //-----------------------
req2.Credentials = new NetworkCredential("username", "passw");              // Creates the Logs folder
req2.Method = WebRequestMethods.Ftp.MakeDirectory;                          //
FtpWebResponse makeDirectoryResponse2 = (FtpWebResponse)req2.GetResponse(); //-----------------------

FtpWebRequest req3 = (FtpWebRequest)WebRequest.Create(custPath3);           //-----------------------
req3.Credentials = new NetworkCredential("username", "passw");              // Creates the Help Folder
req3.Method = WebRequestMethods.Ftp.MakeDirectory;                          //
FtpWebResponse makeDirectoryResponse3 = (FtpWebResponse)req3.GetResponse(); //-----------------------

It worked once, and then never worked again. That's what really boggles me. Anyways, I now get a WebException was unhandled error: Remote server returned an error (550) File unavailable (not found, no access) on line 4 or the first FtpWebResponse makeDirectoryResponse statement.

As soon as the folders are created, I have a file uploaded to one of the folders. That actually works, but only if the folders are already there, so the above issue is holding me up.

Any thoughts?

Thanks,
Hendo

Have you verified that the ftp site allows folder creation?

Also, you can clean the code with something like the following:

private WebResponse CreateDirectory(string sDir)
{
    try {
        FtpWebRequest req = FtpWebRequest.Create(sDir);
        req.Credentials = new NetworkCredential("username", "passw");
        req.Method = WebRequestMethods.Ftp.MakeDirectory;
        WebResponse Response = req.GetResponse;
        return Response;
    } catch (Exception ex) {
        Interaction.MsgBox(ex.ToString());
        return null;
    }
}

Yes, folder creation is allowed.

Ok, please bear with me on this. Since my code is all under the button_click event, do I try your example under that button_click event, and repeat it the 4 times that I neeed?

You would call the code under your button click and pass in the path to create.

I'm sorry. This doesn't work either. I should have stated this earlier...this is a .NET2.0 using VS2010. Not sure if that makes a difference or not.

It appears that in the coversion from VB.NET to C# that the code is not a working C# code.

Try something like this:

private WebResponse CreateDirectory(string sDir)
{
    try {
        FtpWebRequest req = FtpWebRequest.Create(sDir);
        req.Credentials = new NetworkCredential("username", "passw");
        req.Method = WebRequestMethods.Ftp.MakeDirectory;
        WebResponse Response = req.GetResponse;
        return Response;
    } catch (Exception ex) {
        MessageBox.Show(ex.ToString());
        return null;
    }
}

When you say "This does not work either" do you mean that the code doesn't compile or that the code does not behave as wanted?

The code does compile, but does not behave as wanted.

Ok, I got it. The code was good. The problem is in the first folder create statement. The "custPath" variable was trying to create two folders at once. I had to break that down so the folders were created one at a time. It all works good now.

Thanks for the help Begginnerdev. I do appreciate you taking the time.

Hendo

commented: Great job! +8

No problem friend! Glad you could find a resolution!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.