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

76 lines
2.3 KiB
C#

using Microsoft.VisualBasic.FileIO;
namespace Password_Manager
{
static class ConfigFileManager
{
private static string configPath = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg";
#region Config flags
/*Configuration: (format: fieldname=value)
pwdStorePath: path to the folder which contains the .gpg files
recipient: whose public key to use for encryption when a new password is created
*/
private const char DELIMETER = '=';
private const string RECIPIENT = "recipient";
private const string PATH = "pwdStorePath";
#endregion
public static string GetPath()
{
try
{
StreamReader sr = new StreamReader(configPath);
if (sr != null)
{
while (!sr.EndOfStream)
{
string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
{
if (fields[0] == PATH)
{
return fields[1];
}
}
else
{
continue;
}
}
}
else
{
throw new FileNotFoundException();
}
}
catch (FileNotFoundException)
{
CreateDefaultConfig();
}
catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return @"C:\Users\Typo\pass"; //TODO: temporary default value
}
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);
}
}
}
}