Currently on my ASP.NET website when I define a new user I write their data to a .txt file like this:
public class User
{
public string UserName;
public string PassWord;
public string Email;
public string FirstName;
public string LastName;
public string Twitter;
public string FaceBook;
public int Age;
public DateTime DateCreated;
public User(string username, string password, string email, string facebook, string twitter, string firstname, string lastname, int age, DateTime datecreated)
{
UserName = username;
PassWord = password;
Email = email;
FirstName = firstname;
LastName = lastname;
Age = age;
DateCreated = datecreated;
Twitter = twitter;
FaceBook = facebook;
AddToTextFile();
}
public bool CheckUsername(string username, List<User> users)
{
for (int i = 0; i < users.Capacity; i++)
{
if (users[i].UserName == username) return true;
else return false;
}
return false;
}
public bool CheckPassword(string username, string password, List<User> users)
{
for (int i = 0; i < users.Capacity; i++)
{
if (users[i].UserName == username)
{
if (users[i].PassWord == password) return true;
else return false;
}
else return false;
}
return false;
}
private void AddToTextFile()
{
string file = "../users-information.txt";
StreamWriter sw = new StreamWriter(@file);
sw.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}", UserName, PassWord, FirstName, LastName, Email, FaceBook, Twitter, Age, DateCreated);
}
Then on my C# app I use WebClient.DownloadData
to get the data and write it to a file.
private void GetUsers()
{
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//users = serializer.Deserialize<User[]>(Request.Form["users"]);
WebClient client = new WebClient();
byte[] file = client.DownloadData("http://kinected-security.com/users-information.txt");
File.WriteAllBytes(@"../user-information/user-information.txt", file);
string[] lines = File.ReadAllLines("../user-information/user-information.txt");
if (lines.Length != 0)
{
foreach (string line in lines)
{
string[] splited = new string[]{ };
splited = line.Split(new Char[] {' ' });
users.Add(new User(splited[0], splited[1], splited[2], splited[3], splited[4], splited[5], splited[6], int.Parse(splited[7]), DateTime.Parse(splited[8])));
}
}
}
Should I be using WebClient.DownloadString
? Is there a more efficient way to do this?