Witam. Mam pewien problem, może ktoś będzię się orientować jak to naprostować. Mam serwer ftp, chciałbym aby został on pobierany podczas sprawdzania logowania(całosc to prosty chat). Ale pliku nie pobiera. sciaga lub tworzy tylko pusty plik w wskazanej lokalizacji. A pozniej program zawiesza się i pisze że plik jest już otwarty, a nie otwieram tego pliku nigdzie wcześniej.
private void DisconnectFTP()
{
client.DownCompleted -= new FtpClient.DownCompletedEventHandler(client_DownCompleted);
client.UpCompleted -= new FtpClient.UpCompletedEventHandler(client_UpCompleted);
}
private void ConnectFTP()
{
if (ipSerwera != string.Empty & ipSerwera != String.Empty)
{
try
{
string serverName = ipSerwera;
if (serverName.StartsWith("ftp://"))
serverName = serverName.Replace("ftp://", "");
client = new FtpClient(serverName, login, password);
client.DownCompleted += new FtpClient.DownCompletedEventHandler(client_DownCompleted);
client.UpCompleted += new FtpClient.UpCompletedEventHandler(client_UpCompleted);
ipSerwera = client.FtpDirectory;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Wprowadz nazwe serwera FTP", "Błąd");
ipSerwera = string.Empty;
}
}
private void DownloadFile()
{
try
{
string localFile = pathLocal + "\\" + nameFile;
FileInfo fi = new FileInfo(localFile);
if (fi.Exists == false)
{
client.DownloadFileAsync("konta.txt", localFile);
}
else
MessageBox.Show("Plik istnieje");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Błąd");
}
}
public bool CheckDataLog(string login, string pass)
{
ConnectFTP();
DownloadFile();
DisconnectFTP();
#region skroty autoryzacji
string tmpLogin="#LOGIN#";
string tmpPass="#PASS#";
string tmpMail="#MAIL#";
#endregion
#region tablica pomocnicza do wsporzednch
int tmpCounterLogin=0;
int tmpCounterPass=0;
int tmpCounterMail = 0;
int[] tabLocLogin = new int[2];
int[] tabLocPass = new int[2];
int[] tabLocMail = new int[2];
#endregion
string pobranyLogin;
string pobranyMail;
string pobranyPass;
string line;
StreamReader file = new StreamReader(pathLocal+"\\"+nameFile);
while ((line = file.ReadLine()) != null)
{
#region zerowanie wartosci pomocniczych
tmpCounterLogin = 0;
tmpCounterMail = 0;
tmpCounterPass = 0;
pobranyLogin = "";
pobranyMail = "";
pobranyPass = "";
#endregion
for (int i = 0; i < line.Length - 5; i++)
{
#region #LOGIN#
// # L O G I N
if (line[i] == tmpLogin[0])
if(line[i + 1] == tmpLogin[1])
if(line[i + 2] == tmpLogin[2])
if(line[i + 3] == tmpLogin[3])
if(line[i + 4] == tmpLogin[4])
if(line[i + 5] == tmpLogin[5])
if(line[i + 6] == tmpLogin[6])
{
tabLocLogin[tmpCounterLogin] = i;
tmpCounterLogin++;
}
#endregion
#region #MAIL#
// # M A I L #
if ((line[i] == tmpMail[0]) && (line[i + 1] == tmpMail[1]) && (line[i + 2] == tmpMail[2]) && (line[i + 3] == tmpMail[3]) && (line[i + 4] == tmpMail[4]) && (line[i + 5] == tmpMail[5]))
{
tabLocMail[tmpCounterMail] = i;
tmpCounterMail++;
}
#endregion
#region #PASS#
// # P A S S #
if ((line[i] == tmpPass[0]) && (line[i + 1] == tmpPass[1]) && (line[i + 2] == tmpPass[2]) && (line[i + 3] == tmpPass[3]) && (line[i + 4] == tmpPass[4]) && (line[i + 5] == tmpPass[5]))
{
tabLocPass[tmpCounterPass] = i;
tmpCounterPass++;
}
#endregion
}
#region przesuniecia wspołrzednych
tabLocLogin[0] = tabLocLogin[0] + 7;
tabLocMail[0] = tabLocMail[0] + 6;
tabLocPass[0] = tabLocPass[0] + 6;
/*tabLocLogin[1] = tabLocLogin[1] - 1;
tabLocMail[1] = tabLocMail[1] - 1;
tabLocPass[1] =tabLocPass[1]-1;*/
#endregion
#region pobranie danych z pliku
//pobranie hasla
for (int i = tabLocPass[0]; i < tabLocPass[1]; i++)
{
pobranyPass = pobranyPass + line[i];
}
//pobranie maila
for (int i = tabLocMail[0]; i < tabLocMail[1]; i++)
{
pobranyMail = pobranyMail + line[i];
}
//pobranie loginu
for (int i = tabLocLogin[0]; i < tabLocLogin[1]; i++)
{
pobranyLogin = pobranyLogin + line[i];
}
#endregion
#region sprawdzanie czy istnieje login
if (pobranyLogin.Length == login.Length)
{//jezeli dlugosc pobrranego loginu sie zgadza
if (pobranyLogin == login)
{//jezeli login jest taki sam jak
if (pobranyPass == pass)
{//jezeli haslo sie zgadza
file.Close();
return true;
}
}
}
#endregion
}
MessageBox.Show("Złe hasło lub login");
file.Close();
return false;
}
Wiem, że jest burdel i ogólnie może wydawać się nie czytelny, na usprawiedliwienie powiem, że to moje pierwsze spotkanie z c#