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

New Topic/Question
Reply




MultiQuote




|