如何将数据存储在缓冲区中? [英] How do I store data in a buffer?
本文介绍了如何将数据存储在缓冲区中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将数据存储在缓冲区中?数据来自ftp服务器,以下代码如下:
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);
推荐答案
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.
是的,它也可以由Stream.Read(newbytearray,start,numberofbytestoread)
Yes and it can also be processed by Stream.Read( newbytearray , start, numberofbytestoread)
处理.
这篇关于如何将数据存储在缓冲区中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文