Fórmulas e Cenas

Object Reference Not Set to Instance of an Object

  Home  |   Contact  |   Syndication    |   Login
  40 Posts | 0 Stories | 7 Comments | 0 Trackbacks

News

Archives

Post Categories

Links

Thursday, October 07, 2010 #

 FileInfo fileInf = new FileInfo("c:\myfile.txt");

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPSite/myfile.txt"));

reqFTP.Credentials = new NetworkCredential("myUser","myPassword", "myFTPDomain");
 

reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();

try
{
    Stream strm = reqFTP.GetRequestStream();
    contentLen = fs.Read(buff, 0, buffLength);
    while (contentLen != 0)
    {
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }
    strm.Close();
    fs.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

 

Enjoy :)


FileStream outputStream = new FileStream("c:\myfilefolder\file.txt");
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPSite/file.txt"));

reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("myUser","MyPassword","FTPDomain");

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();

int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
    outputStream.Write(buffer, 0, readCount);
    readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
outputStream.Close();
response.Close();

 

Enjoy :)


FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myFTPURL/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("myUser", "myPassword", "FTPDomain");

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while (line != null)
{
    line = reader.ReadLine();
}

reader.Close();
response.Close();

 

Enjoy :)