我的应用程序的目的是,它必须将文件上传到 FTP 服务器,然后将本地文件移动到存档文件夹。这是我的代码:
public void UploadLocalFiles(string folderName)
{
try
{
string localPath = @"\\Mobileconnect\filedrop_to_ssis\" + folderName;
string[] files = Directory.GetFiles(localPath);
foreach (string filepath in files)
{
string fileName = Path.GetFileName(filepath);
localFileNames = files;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp:...../inbox/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.ServicePoint.ConnectionLimit = files.Length;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = Certificate;
FileInfo fileInfo = new FileInfo(localPath + @"\" + fileName);
byte[] fileContents = new byte[fileInfo.Length];
FileStream fileStream = fileInfo.OpenRead();
fileStream.Read(fileContents, 0, Convert.ToInt32(fileInfo.Length));
Stream writer = reqFTP.GetRequestStream();
writer.Write(fileContents, 0, fileContents.Length);
}
reqFTP.Abort();
}
catch (Exception e)
{
Console.WriteLine("Error in GetLocalFileList method!!!!!" + e.Message);
}
}
运行此方法后,我无法移动文件,我收到此异常消息:“无法访问文件,该文件正在被另一个进程使用”。是我的 Filestream 或 Stream 锁定了我的文件。当我包围 Filestream 和 Stream 时,using
它不会像没有将文件上传到 FTP 那样using
。我不明白为什么,有人可以帮忙吗?