In this post I am using FTP to upload or download a folder or file into server
FtpWebRequest Class
using System;
using System.Net;
using System.Threading;
using System.IO;
namespace Examples.System.Net
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;
public FtpState()
{
wait = new ManualResetEvent(false);
}
public ManualResetEvent OperationComplete
{
get {return wait;}
}
public FtpWebRequest Request
{
get {return request;}
set {request = value;}
}
public string FileName
{
get {return fileName;}
set {fileName = value;}
}
public Exception OperationException
{
get {return operationException;}
set {operationException = value;}
}
public string StatusDescription
{
get {return status;}
set {status = value;}
}
}
public class AsynchronousFtpUpLoader
{
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;
Uri target = new Uri (args[0]);
string fileName = args[1];
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example uses anonymous logon.
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
// Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName;
// Get the event to wait on.
waitObject = state.OperationComplete;
// Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback (EndGetStreamCallback),
state
);
// Block the current thread until all operations are complete.
waitObject.WaitOne();
// The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine ("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback (EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
}
}
// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse) state.Request.EndGetResponse(ar);
response.Close();
state.StatusDescription = response.StatusDescription;
// Signal the main application thread that
// the operation is complete.
state.OperationComplete.Set();
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine ("Error getting response.");
state.OperationException = e;
state.OperationComplete.Set();
}
}
}
}
public static bool DeleteFileOnServer(Uri serverUri) { // The serverUri parameter should use the ftp:// scheme. // It contains the name of the server file that is to be deleted. // Example: ftp://contoso.com/someFile.txt. // if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); request.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse) request.GetResponse(); Console.WriteLine("Delete status: {0}",response.StatusDescription); response.Close(); return true; }
elazığ
ReplyDeletebilecik
kilis
sakarya
yozgat
KSS
goruntulu show
ReplyDeleteücretli
43S
kayseri evden eve nakliyat
ReplyDeletesamsun evden eve nakliyat
muğla evden eve nakliyat
bursa evden eve nakliyat
bingöl evden eve nakliyat
J1N8EF
D23EF
ReplyDeleteTokat Parça Eşya Taşıma
Şırnak Evden Eve Nakliyat
Yalova Şehir İçi Nakliyat
Çerkezköy Mutfak Dolabı
Ünye Marangoz
Kilis Şehir İçi Nakliyat
Rize Şehir İçi Nakliyat
Iğdır Parça Eşya Taşıma
Sivas Parça Eşya Taşıma
50622
ReplyDeleteAydın Şehir İçi Nakliyat
Hotbit Güvenilir mi
Rize Şehir İçi Nakliyat
Bolu Parça Eşya Taşıma
Gümüşhane Şehirler Arası Nakliyat
Ağrı Şehirler Arası Nakliyat
Trabzon Parça Eşya Taşıma
Tekirdağ Boya Ustası
Van Lojistik
00CC6
ReplyDeleteÇerkezköy Oto Elektrik
Bayburt Şehir İçi Nakliyat
Iğdır Şehirler Arası Nakliyat
Adana Şehir İçi Nakliyat
Ardahan Parça Eşya Taşıma
Muş Lojistik
Btcturk Güvenilir mi
Probit Güvenilir mi
Kütahya Evden Eve Nakliyat
591A4
ReplyDeleteAnkara Evden Eve Nakliyat
parabolan for sale
winstrol stanozolol for sale
buy trenbolone enanthate
for sale dianabol methandienone
Bingöl Evden Eve Nakliyat
Kastamonu Evden Eve Nakliyat
order pharmacy steroids
Muş Evden Eve Nakliyat
54143
ReplyDeleteısparta canlı sohbet bedava
mardin canlı sohbet uygulamaları
uşak mobil sesli sohbet
çanakkale en iyi rastgele görüntülü sohbet
sinop bedava sohbet
eskişehir görüntülü sohbet uygulama
izmir canlı sohbet uygulamaları
Çankırı Yabancı Canlı Sohbet
rastgele görüntülü sohbet uygulamaları
23FD6
ReplyDeletemalatya mobil sohbet siteleri
kadınlarla rastgele sohbet
malatya canlı sohbet bedava
bursa yabancı görüntülü sohbet uygulamaları
mobil sohbet chat
Batman Ücretsiz Sohbet Uygulaması
Ardahan Sesli Mobil Sohbet
görüntülü sohbet ücretsiz
tokat mobil sohbet et