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/ConfigFileManager.cs

99 lines
2.8 KiB
C#
Raw Normal View History

using Microsoft.VisualBasic.FileIO;
namespace Password_Manager
{
static class ConfigFileManager
{
#region Config flags
/*Configuration: (format: fieldname=value)
password_store_path: path to the folder which contains the .gpg files
recipient: whose public key to use for encryption when a new password is created
*/
private static string CONFIGPATH = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg";
private static char DELIMETER = '=';
private static string RECIPIENT = "recipient";
private static string PATH = "password_store_path";
#endregion
private static Config Configuration;
public static void Init()
{
try
{
StreamReader sr = new StreamReader(CONFIGPATH);
string? path = null, recipient = null;
if (sr != null)
{
while (!sr.EndOfStream)
{
string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
{
if (fields[0] == PATH)
{
2023-03-28 13:05:18 +02:00
path = fields[1];
}
else if (fields[0] == RECIPIENT)
{
2023-03-28 13:05:18 +02:00
recipient = fields[1];
}
}
else //probably an empty line or something
{
continue;
}
}
if (path != null && recipient != null)
{
Configuration = new Config(path, recipient);
}
else
{
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
}
}
else
{
throw new FileNotFoundException();
}
}
catch (FileNotFoundException)
{
CreateDefaultConfig();
}
catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static string GetPath()
{
return Configuration.PasswordStorePath;
}
public static string GetRecipient()
{
return Configuration.Recipient;
}
private static void CreateDefaultConfig()
{
try
{
StreamWriter sw = new StreamWriter(CONFIGPATH);
sw.WriteLine(RECIPIENT + DELIMETER + "typo");
sw.WriteLine(PATH + DELIMETER + @"C:\Users\Typo\pass");
sw.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}