我无法将我的 Stream 转换为 MemoryStream。我想这样做,因为我想删除我上传到 FTP 服务器的文件。当我尝试删除文件或将文件移动到另一个文件夹时,我收到一个异常,告诉我该文件正在被另一个进程使用。此应用程序的目的是将文件上传到 FTP 服务器,并将文件移动到存档文件夹。这是我的代码:
public void UploadLocalFiles(string folderName)
{
try
{
string localPath = @"\\Mobileconnect\filedrop_to_ssis\" + folderName;
string[] files = Directory.GetFiles(localPath);
string path;
foreach (string filepath in files)
{
string fileName = Path.GetFileName(filepath);
localFileNames = files;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp:......./inbox/" + fileName));
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.ServicePoint.ConnectionLimit = files.Length;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = Certificate;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileInfo = new FileInfo(localPath + @"\" + fileName);
FileStream fileStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = reqFTP.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);
var memoStream = new MemoryStream();
uploadStream.CopyTo(memoStream);
memoStream.ToArray();
uploadStream.Close();
while (contentLength != 0)
{
memoStream.Write(buffer, 0, bufferLength);
contentLength = fileStream.Read(buffer, 0, bufferLength);
}
}
reqFTP.Abort();
}
catch (Exception e)
{
Console.WriteLine("Error in GetLocalFileList method!!!!!" + e.Message);
}
}
当我到达这行代码时:
uploadStream.CopyTo(memoStream);
我收到一个异常,告诉我这个 Stream 无法读取。
我该如何解决这个问题?