我正在使用c#中的ftp应用程序,我必须下载文件并上传相同的文件。这是我下载数据的代码。
try
{
textBox1.Text = ftpServerIP;
textBox2.Text = ftpUserID;
textBox3.Text = ftpPassword;
FtpWebRequest reqFTP;
//filePath = <<The full path where the file is to be created.>>,
//fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
Uri downloadPath = new Uri("ftp://" + ftpServerIP + "/" + client_fileName);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(downloadPath);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
DataBaseOperations db = new DataBaseOperations();
templist = db.GetData();
dataGridView1.DataSource = templist;
dataGridView1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我的上传功能是
public void Upload(string filename, string url)
{
FileInfo fileInf = new FileInfo(filename+"\\test.s3db");
string uri = "ftp://" + url + "/" +"/public_html/RemoteDic/" + " test.s3db";
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
FileStream inputStream = fileInf.OpenRead();
using (var outputStream = reqFTP.GetRequestStream())
{
var buffer = new byte[1024];
int totalReadBytesCount = 0;
int readBytesCount;
while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, readBytesCount);
totalReadBytesCount += readBytesCount;
var progress = totalReadBytesCount * 100.0 / inputStream.Length;
}
outputStream.Close();
}
inputStream.Close();
}
当我运行程序时出现错误
该进程无法访问文件
'E:\rafay zia mir\web connect\web connect\bin\Debug\test.s3db'
,因为它正由另一个进程使用。
但是在下载部分中,所有流都在文件下载后关闭。