2013-07-12 2 views
8

내 프로그램 :C#으로 FTP에서 파일을 삭제이 코드를 사용하여 FTP 서버에 파일을 업로드 할 수 있습니다

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

을 마우스 오른쪽 단추로 지금은 일부 파일을 삭제해야 내가 바로 그렇게 할 수 없습니다.

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

답변

32

대신에 클래스를 사용해야합니다.

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

당신이 파일을 삭제해야 할 때하는 FtpWebRequest를 사용해야합니다

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

심판 : 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; 
    }    
} 

사용법 :

DeleteFileOnFtpServer(new Uri (toDelFname), user,pass);