我要通过代码删除Ftp主机上的某个或某些文件 
ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;
往下怎么些呀

解决方案 »

  1.   

            /// <summary>
            /// 删除文件
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="cred">通行证</param>
            /// <returns></returns>
            public static bool FtpDeleteFile(string path, ICredentials cred)
            {
                FtpWebRequest req = (FtpWebRequest)WebRequest.Create(path);
                if (cred != null)
                    req.Credentials = cred;
                req.Method = WebRequestMethods.Ftp.DeleteFile;
                req.KeepAlive = false;            try
                {
                    req.GetResponse().Close();
                    return true;
                }
                catch (WebException ex)
                {
                    log.Error("FtpDeleteFile Error:" + ex.Message);
                    return false;
                }
            }
      

  2.   

    运行到  FtpWebRequest req = (FtpWebRequest)WebRequest.Create(path);
    就提示错误了:无法将类型为“System.Net.FileWebRequest”的对象强制转换为类型“System.Net.FtpWebRequest”。
    我的path是\\192.168.1.55\f$\test.txt
      

  3.   

    去下载个FTPclient的类.
    就是一个类文件.很好用的.
    里边提供了很多方法.包括你所要的删除
      

  4.   

    或直接用.net中自带的 FtpWebRequest 类也可以实现
    有密码的话需要添加FtpWebRequest的Credentials属性,参考msdn
    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;
    }
      

  5.   


    你这个path哪是ftp的路径呀..晕
    unc路径和ftp路径分不清楚吗?
      

  6.   

    [email protected]
    给我发个FTPclient的类.