Witam,
kod realizuje wgrywanie plików xml z danej lokalizacji na dysku na serwer ftp do danej lokalizacji -> wgrywanie działa
-
nie wiem dlaczego gdy wgrywam np. dwa pliki do pliku log dodaje jeden wpis a chciałbym dwa ( po jednym dla pliku ) i kod mi na to wygląda i nie działa :)
-
gdy odkomentuję File.Delete(name) nie kasuje pliku
co robię nie tak?
/* uploadFile method uploads files from specific folder to FTP Srv
*
* @params
* --------
* FTPAddress - address of FTP server
* filePath - path from where application uploads files
* username - user credentials to log in to FTP Srv
* password - pass to log in to FTP Srv
*
* $return
* -------
* method writes results to log file
*/
private void uploadFile(string FTPAddress, string filePath, string username, string password)
{
//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/test/" + Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Load the file
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//Upload file
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
// write to log file
//writeLog();
MessageBox.Show("Uploaded Successfully");
}
/* writeLog method writes results of upload to file
*
* $return
* -------
* method writes results to log file
*/
private void writeLog()
{
string strLogText = "Some details you want to log.";
// Create a writer and open the file:
StreamWriter log;
if (!File.Exists(@"C://aa_ftp_test/logfile.txt"))
{
log = new StreamWriter(@"C://aa_ftp_test/tms_logfile.txt");
}
else
{
log = File.AppendText(@"C://aa_ftp_test/tms_logfile.txt");
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strLogText);
log.WriteLine();
log.WriteLine("-------------------------------------------------------");
log.WriteLine();
// Close the stream:
log.Close();
}
private void btnUpload_Click_1(object sender, EventArgs e)
{
Application.DoEvents();
string[] filePaths = Directory.GetFiles(@"C://aa_ftp_test", "*.xml");
foreach (string name in filePaths)
{
uploadFile(txtFTPAddress.Text, name, txtUsername.Text, txtPassword.Text);
//File.Delete(name);
}
}