Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I store my data in a buffer? The data comes from a ftp server the following code is below:



using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;


namespace ProjectEnvironmentVariableUpd
{
    class Program
    {
        static void Main(string[] args)
        {



            BasicFTPClient MyClient = new BasicFTPClient();

            MyClient.Host = "[removed to protect user]";
            MyClient.Username = "[removed to protect user]";
            MyClient.Password = "[removed to protect user]";

            try
            {
                MyClient.DownloadFile("test.txt"); // c:\temp\mydownload.txt for Windows
            }
            catch (WebException e)
            {
                Console.WriteLine(e.ToString());
            }
        }

    }

    class BasicFTPClient
    {
        public string Username;
        public string Password;
        public string Host;
        public int Port;

        public BasicFTPClient()
        {
            Username = "[removed to protect user]";
            Password = "[removed to protect user]";
            Port = 21;
            Host = "[removed to protect user]";
        }

        public BasicFTPClient(string theUser, string thePassword, string theHost)
        {
            Username = theUser;
            Password = thePassword;
            Host = theHost;
            Port = 21;
        }

        private Uri BuildServerUri(string Path)
        {
            return new Uri(String.Format("ftp://{0}:{1}/{2}", Host, Port, Path));
        }


        /// <summary>
        /// This method downloads the given file name from the FTP server
        /// and returns a byte array containing its contents.
        /// Throws a WebException on encountering a network error.
        /// </summary>

        public string DownloadData(string path)
        {
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            
            // return a string

            byte[] data = request.DownloadData(BuildServerUri(path));
            
            string data2 ="";

            // see a other solution to convert byte into string

            for (int i=0;i <data.Length;i++)
            {
             data2 += (char)data[i]; 
            }
            return data2;
        }


        /// <summary>
        /// This method downloads the FTP file specified by "ftppath" and saves
        /// it to "destfile".
        /// Throws a WebException on encountering a network error.
        /// </summary>
        public void DownloadFile(string ftppath)
        {
            // Download the data
            string Data = DownloadData(ftppath);


            // convert data in string




            // Save the data to RAM in buffer
            //buffer size was automatically set to the default size of the defined data buffer, 1024 byte
            string[] Data = new byte[1024];
           


            // download data file test.txt in ram buffer and add or del enviroment variable



            // be carreful don't erase other values in environement variable

           
            
            Console.WriteLine(Data);
        }

            
        // get enviroment variables in Data and their value and action





        static void ShowEnvironmentVariables()
        {
            // Get and display the value of PATH
            string sPathValue = Environment.GetEnvironmentVariable("path");
            Console.WriteLine("PATH = {0}", sPathValue);

            // Show all environment variables
            Console.WriteLine("Machine environment variables:");
            IDictionary env = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);
            foreach (DictionaryEntry var in env)
            {
                Console.WriteLine("\t{0} = {1}", var.Key, var.Value);
            }
        }
   
        
        // update / dell / add enviroment variables in Data and their value include action



            //Console.ReadKey(true);
Posted
Updated 7-Jun-10 5:19am
v3
Comments
OriginalGriff 7-Jun-10 9:22am    
Rather than just posting you entire program with a "How do I store data in a buffer" question, cut it down to the bit you are having problems with and ask a specific question. At the moment, it looks like you have a method called "DownloadFile" which does nothing and you are expecting us to write it for you! But that would be silly, wouldn't it? :laugh:
Dylan Morley 7-Jun-10 9:45am    
You should probably remove your host name from the code you've posted....there are evil people out there.

Yes and it can also be processed by Stream.Read( newbytearray , start, numberofbytestoread)
 
Share this answer
 
C#
public void DownloadFile(string ftppath)     
 {            
    string Data = DownloadData(ftppath);
    System.Text.Encoding enc = System.Text.Encoding.ASCII;
    byte[] myByteArray = enc.GetBytes(Data);
}


After this the bytearray can be either stored in memorystream or filestream.
 
Share this answer
 
v2
Comments
CoderChia 9-Jun-10 9:27am    
so basically i am creating a new array or buffer called mybytearray and get the variable data from it?

what is exactly System.Text.Encoding enc = System.Text.Encoding.ASCII;
CoderChia 9-Jun-10 9:38am    
so this bytearray is it a buffer or just a plain array i do not get what you mean

"After this the bytearray can be either stored in memorystream or filestream."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900