6 Replies - 6169 Views - Last Post: 28 August 2012 - 04:49 PM Rate Topic: -----

#1 qwertyuu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 31-July 12

FTP: Create directory and file with only one FtpWebRequest?

Posted 27 August 2012 - 10:05 PM

Okay, found out how cool/useful FTP was and I decided to set a server up on my dedicated server machine I built last year.
Everything works well etc.

My problem is: in the code I'm using, I need to create 2 different "FtpWebRequest"... one for creating the directory of where the file will be and a second one for the file creation itself.
I would love if someone could tell me how I could write only 1 FtpWebRequest so my code creates the Dir and the File in one shot.

For now, it only supports text transmission, but I'll mess with other, more advanced features later.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Test_FTP
{
    class Program
    {
        static void Main(string[] args)
        {
            string pcName = "Pc-Name";
            FtpWebRequest create = (FtpWebRequest)WebRequest.Create("ftp://MYCENSOREDSEREVER.com/" + pcName);
            create.Method = WebRequestMethods.Ftp.MakeDirectory;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://MYCENSOREDSERVER.com/"+ pcName +"/file.txt");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            

            request.Credentials = new NetworkCredential("user", "pass");
            create.Credentials = request.Credentials;
            var resp = (FtpWebResponse)create.GetResponse();

            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();
        }
    }
}


PS: this code works, but I'm sure there's a way to get that done in 1 shot... Or is there?

This post has been edited by qwertyuu: 27 August 2012 - 10:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: FTP: Create directory and file with only one FtpWebRequest?

#2 qwertyuu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 31-July 12

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 01:04 PM

Nevermind then?
first disappointment in the forum yet.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4485
  • View blog
  • Posts: 7,813
  • Joined: 08-June 10

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 01:08 PM

What do you want from us? There's no single FTP command that creates a directory and uploads a file. You're going to have to make two requests one way or another.

You could rework it so that you still use the same object, but you'll still have to send it two separate times.

Also: a whole 12 hours and you're "disappointed"? Cry me a river.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 01:30 PM

If you can find a single command in the FTP RFC 959 that will let you create a folder and upload the file at the same time, you can set the WebRequest.Method value to that string.

As Curtis Rutland had said though, the protocol currently requires two commands to be sent.

Perhaps, you should investigate if your file/web server supports WebDAV. I'm not sure, but I was under the impression that WebDAV lets you create items on the fly and will create any intervening paths as needed. At least that was the elevator pitch I heard from somebody as they were trying to sell me on the merits of WebDAV. I've not looked any further into the veracity of his claims.
Was This Post Helpful? 0
  • +
  • -

#5 qwertyuu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 31-July 12

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 02:04 PM

View PostCurtis Rutland, on 28 August 2012 - 01:08 PM, said:

Also: a whole 12 hours and you're "disappointed"? Cry me a river.

Yes, I am.

View PostSkydiver, on 28 August 2012 - 01:30 PM, said:

If you can find a single command in the FTP RFC 959 that will let you create a folder and upload the file at the same time, you can set the WebRequest.Method value to that string.

As Curtis Rutland had said though, the protocol currently requires two commands to be sent.

Perhaps, you should investigate if your file/web server supports WebDAV. I'm not sure, but I was under the impression that WebDAV lets you create items on the fly and will create any intervening paths as needed. At least that was the elevator pitch I heard from somebody as they were trying to sell me on the merits of WebDAV. I've not looked any further into the veracity of his claims.


I'm running a simple FileZilla server. Is there a way I can get to know if I use this WebDAV... or to use it?

Also, problem half fixed... I created a method that checks if an error is given while creating the dir (dir already exists).. if that happens it goes on creating the file, otherwise it creates the directory.
The only flaw in using 2 different FtpWebRequest is that the program has to do 2 different connections one after the other; one for the directory, second one for the file.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 02:10 PM

Since you are running your own server, why not download the source code, modify it to add a custom command like "CSTR" for "create and store", and then you'll have your way to upload and create the directory as needed?
Was This Post Helpful? 1
  • +
  • -

#7 qwertyuu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 31-July 12

Re: FTP: Create directory and file with only one FtpWebRequest?

Posted 28 August 2012 - 04:49 PM

View PostSkydiver, on 28 August 2012 - 02:10 PM, said:

Since you are running your own server, why not download the source code, modify it to add a custom command like "CSTR" for "create and store", and then you'll have your way to upload and create the directory as needed?

That wouldn't be compatible with FTP server in general.
But it still is a good idea, though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1