?
Solved

Use FTP to Copy a file to Another Folder

Posted on 2013-11-27
8
Medium Priority
?
380 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 2000 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

765 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