views:

272

answers:

1

This is what I have right now for a file residing on the same server and it works.

Dim FILENAME as String = Server.MapPath("Output.txt")

Dim objStreamWriter as StreamWriter
objStreamWriter = File.CreateText(FILENAME)

dr = myCommand.ExecuteReader()
While dr.Read()

objStreamWriter.WriteLine("{0}|{1}|{2:yyyy-MM-dd}|{3:yyyy-MM-dd}", dr(0),  dr(1), dr(2), dr(3))

End While
objStreamWriter.Close()

I was planning on FTPing the file to another server once done but is there a way to do it on a remote server? i.e create a text file on a remote server and write to it? Did not find any instances of that usage. How to use ServerXMLHTTP to replace Server.MapPath in this instance?

Thank you so much for your time.

+1  A: 

Using MapPath and StreamWriter this way won't work. This is meant for a local file scenario. You can directly FTP files from .net using FtpWebRequest.

Here's an example of using FtpWebRequest to write a file (Example from here):

        FtpWebRequest request =
            (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/output.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = 
            new NetworkCredential ("anonymous","[email protected]");

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(stuff); // write your stuff here
        requestStream.Close();

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

        Console.WriteLine("Upload File Complete, status {0}",
            response.StatusDescription);

        response.Close();
Keltex
Thanks for the solution. This output file is created at regular intervals. How to delete the file remotely and put the newly created file there?
ThinkCode
@NHTechGuy - Look at this link: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx There's an example of how to delete a remote file also.
Keltex