Solved

Use FTP to Copy a file to Another Folder

Posted on 2013-11-27
8
375 Views
Last Modified: 2013-11-28
I am attempting to copy a tab delimited text file to an FTP folder.

This is the example I am using: http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx

// Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("Changes.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();

error: The name Encoding does not exist in the current context.

I am not sure that  byte [] fileContents  applies to a tab delimited file.

This is the structure of Changes.txt:

     NameChangeDate           Market  OldName NewName
     2009-04-15 00:00:00.000      NASDAQ      SGIC      SGICQ
     2009-04-16 00:00:00.000      AMEX      ABH          ABWTQ
     2009-04-16 00:00:00.000      NYSE      GGP          GGWPQ
     2009-04-17 00:00:00.000      NASDAQ      XTLB      XTLBY
     2009-04-20 00:00:00.000      NASDAQ      ATTC      ATTCE

What is the correct syntax for reading the file contents?

Thanks
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 39680613
I think you have just forgot
using System.Text;

Open in new window

in your code.

Oli
0
 

Author Comment

by:Dovberman
ID: 39680771
Yes, That was simple. Thanks.

The example describes how to copy from one FTP folder to another.

I need to copy the file from my local machine folder to the FTP folder.

I know how to create the local filestream.

What do I need to change in the upload statement?

Or is this the subject of a new question?

Thanks
0
 

Author Closing Comment

by:Dovberman
ID: 39680939
Thank you.
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 9

Expert Comment

by:oheil
ID: 39680943
You are welcome.
0
 
LVL 9

Expert Comment

by:oheil
ID: 39680954
Odd: I posted some code for the upload, but it is not in the thread... maybe I didnt hit the Submit button? But I think it is clear from your own example which you linked above.

Have a good time,

Oli
0
 

Author Comment

by:Dovberman
ID: 39681245
Would you mind posting the upload code again?

Thanks
0
 
LVL 9

Expert Comment

by:oheil
ID: 39682973
Sure. I posted (or not):

Its not subject to a new question.

From your example above it would be something like:

           using System.Net;



           // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://your.server.com/some/path/your_file.txt");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

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

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
    
            response.Close();

Open in new window


The line
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
must be changed to your credentials.
Syntax is: NetworkCredential( String user, String password);

If everything is good, it should create the file "your_file.txt" on the server "your.server.com" in the directory "some/path/".

Oli
0
 

Author Comment

by:Dovberman
ID: 39684006
Thank you. This is how it worked for me.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question