Solved

Use FTP to Copy a file to Another Folder

Posted on 2013-11-27
8
362 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
  • 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
 
LVL 9

Expert Comment

by:oheil
ID: 39680943
You are welcome.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now