This repository has been archived on 2025-09-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Password-Manager-Legacy/Password Manager/Profiles/ProfileHandler.cs

90 lines
2.8 KiB
C#
Raw Normal View History

2023-03-27 00:19:30 +02:00
using Password_Manager;
using Microsoft.VisualBasic.FileIO;
2023-03-24 14:03:09 +01:00
2023-03-27 00:19:30 +02:00
namespace Profiles
2023-03-24 14:03:09 +01:00
{
static class ProfileHandler
{
public static Profile CurrentProfile;
public static ProfileList ListOfProfiles;
2023-03-27 00:19:30 +02:00
private static string filePath = SpecialDirectories.CurrentUserApplicationData + "Profiles.csv";
2023-03-24 14:03:09 +01:00
2023-03-27 00:19:30 +02:00
public static void ChangeProfiles(string profileName)
2023-03-24 14:03:09 +01:00
{
2023-03-27 00:19:30 +02:00
CurrentProfile = ListOfProfiles.SearchByName(profileName);
}
public static void Init()
{
ListOfProfiles = new ProfileList();
if (File.Exists(filePath))
{
LoadProfiles();
}
else
{
CreateProfiles();
}
}
private static void LoadProfiles()
{
try
{
StreamReader sr = new StreamReader(filePath);
string[] lines = sr.ReadToEnd().Split("\n");
foreach (string line in lines)
{
string[] fields = line.Split(',');
try
{
ListOfProfiles.Add(new Profile(fields[0], fields[1]));
} catch (IndexOutOfRangeException)
{
//File is messed up, creating new one
sr.Close();
CreateProfiles();
break;
}
}
sr.Close();
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void TrimLastLine()
{
List<string> lines = File.ReadAllLines(filePath).ToList();
File.WriteAllLines(filePath, lines.GetRange(0, lines.Count - 1).ToArray());
}
private static void CreateProfiles()
{
try
{
StreamWriter sw = new StreamWriter(filePath);
string[] parameters = new string[2];
NewProfileForm np = new NewProfileForm();
Profile firstProfile = null;
np.NewProfileRequest += (string profileName, string profilePath) =>
{
firstProfile = new Profile(profileName, profilePath);
ListOfProfiles.Add(firstProfile);
ChangeProfiles(firstProfile.Name);
sw.WriteLine($"{firstProfile.Name},{firstProfile.Path}");
sw.Close();
TrimLastLine();
};
np.ShowDialog();
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2023-03-24 14:03:09 +01:00
}
}
}