通过FtpWebRequest上传文件到FTP后无法删除

本文关键字:删除 FTP FtpWebRequest 文件 通过 | 更新日期: 2023-09-27 18:03:51

下面是我上传文件的代码:

                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential("****", "*****");
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.KeepAlive = false;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = true;
                reqFTP.ContentLength = stream.Length;
                // reqFTP.EnableSsl = true; // it's FTPES type of ftp
                int buffLen = 2048;
                byte[] buff = new byte[buffLen];
                int contentLen;
                try
                {
                    Stream ftpStream = reqFTP.GetRequestStream();
                    contentLen = stream.Read(buff, 0, buffLen);
                    while (contentLen != 0)
                    {
                        ftpStream.Write(buff, 0, contentLen);
                        contentLen = stream.Read(buff, 0, buffLen);
                    }
                    ftpStream.Flush();
                    ftpStream.Close();
                    ftpStream.Dispose();
                }
                catch (Exception exc)
                {
                    return false;
                }
                //delete image from local
                stream.Flush();
                stream.Close();
                stream.Dispose();
                DeleteFile();

DeleteFile方法尝试删除上传的文件;但它有一个错误,文件正在被我的应用程序使用,所以它不能删除它。有人能帮我解决这个问题吗?

UPDATE1 :

private void DeleteFile()
    {
        DirectoryInfo DirInfo = new DirectoryInfo(@"Direectory Contains uploaded file");
        foreach (FileInfo file in DirInfo.GetFiles())
        {
            file.Delete();
        }
    }

通过FtpWebRequest上传文件到FTP后无法删除

我假设你想删除本地文件?错误提示文件仍在使用中。将初始化流变量的代码包装在using块中