0

My wpf application has a login screen. The login screen consist of a combobox with a collection of people. I currently implemented a folder directory and text file creating upon initialization(if first time login) once the folder and file exist, it reads the file and based on the value that's saved, it'll load the correct user from the collection of people.

My question is this the most ideal way to do it? I'm wondering is there a better way to do it, I feel like this is a very old school method. Any tips would be greatly appreciated.

For example, if John Smith logs in - next time John starts the application, his name will automatically be the SelectedItem.

public void CacheInitialize()
{
    var folderDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "Cache";
    var fileName = "UserCache.txt";
    var fileDirectory = folderDirectory + "\\" + fileName;
    if (!Directory.Exists(folderDirectory))
    {
        Directory.CreateDirectory(folderDirectory);
    }
    if (!File.Exists(fileDirectory))
    {
        File.WriteAllText(Path.Combine(folderDirectory, fileName), string.Empty);
        return;
    }
    else
    {
        var filecontent = File.ReadAllText(Path.Combine(folderDirectory, fileName));
        if (filecontent != string.Empty)
            SelectedUser = Users.First(x => x.Id == int.Parse(filecontent));
    }
}

public void CacheSave()
{
    var folderDirectory = AppDomain.CurrentDomain.BaseDirectory + "Cache";
    var fileName = "cache file3.txt";
    var fileDirectory = folderDirectory + "\\" + fileName;

    StreamWriter writer = new StreamWriter(fileDirectory);
    writer.Write(SelectedUser.Id.ToString());
    writer.Close();

}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Master
  • 2,038
  • 2
  • 27
  • 77
  • The immediate answer to me is to use a transactional database of some sort. This file stuff is asking for trouble! Guess it really depends on the context of how the app is used and how people connect with it. Does this help? http://stackoverflow.com/questions/3784477/c-sharp-approach-for-saving-user-settings-in-a-wpf-application – Trevor Ash Oct 23 '15 at 03:50

1 Answers1

2

There is no advantage in creating new file, especially in non-formatted text format. I would use App.config to save these values:

public void CacheInitialize()
{
    string lastLogin = ConfigurationManager.AppSettings["LastLogin"];
    if (String.IsNullOrEmpty(lastLogin)) return;        

    int userId;
    if (!int.TryParse(lastLogin, out userId)) 
        throw new ArgumentException("LastLogin is not an integer.");

    SelectedUser = Users.First(x => x.Id == userId);
}

public void CacheSave()
{
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    if (configuration.AppSettings.Settings["LastLogin"] == null)
        configuration.AppSettings.Settings.Add("LastLogin", SelectedUser.Id.ToString());
    else
        configuration.AppSettings.Settings["LastLogin"].Value = SelectedUser.Id.ToString();
    configuration.Save();

    ConfigurationManager.RefreshSection("appSettings");
}

Please, be careful with this code:

SelectedUser = Users.First(x => x.Id == int.Parse());

Remember that the end user can replace the value in your file and he shouldn't get access to application as another user without proper authentication.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101