Solved

Use FTP to Copy a file to Another Folder

Posted on 2013-11-27
8
372 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

732 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