2013-07-12 18 views
8

Programım:C# ftp dosya silmek, bu kodu kullanarak bir ftp sunucusuna dosya yükleyebilir

WebClient Client = new WebClient(); 
Client.Credentials = new System.Net.NetworkCredential(ftpUsername, ftpPassword); 
Client.BaseAddress = ftpServer; 
Client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName); 

Şu anda bazı dosyaları silmeniz gerekir ve doğru bunu yapamam. Ben

Client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName); 

cevap

32

yerine kullanmalıyım Ne, sanırım bunu sınıfını kullanmanız gerekecektir.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); 

//If you need to use network credentials 
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword); 
//additionally, if you want to use the current user's network credentials, just use: 
//System.Net.CredentialCache.DefaultNetworkCredentials 

request.Method = WebRequestMethods.Ftp.DeleteFile; 
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
Console.WriteLine("Delete status: {0}", response.StatusDescription); 
response.Close(); 
1

Eğer dosyaları silmek gerektiğinde FtpWebRequest kullanmalıdır:

// 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(); 

ref: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

2
public static bool DeleteFileOnServer(Uri serverUri) 
{ 

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; 
} 
9
public static bool DeleteFileOnFtpServer(Uri serverUri, string ftpUsername, string ftpPassword) 
{ 
    try 
    { 
     // 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.Credentials = new NetworkCredential(ftpUsername, ftpPassword); 
     request.Method = WebRequestMethods.Ftp.DeleteFile; 

     FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
     //Console.WriteLine("Delete status: {0}", response.StatusDescription); 
     response.Close(); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     return false; 
    }    
} 

Kullanımı:

DeleteFileOnFtpServer(new Uri (toDelFname), user,pass); 
İlgili konular