1
0

Using AppContext.BaseDirectory - as Assembly.Location might not be available in single-file applications.

This commit is contained in:
2022-06-20 18:55:18 +02:00
parent 65bca0a922
commit 42ae72a1b7
3 changed files with 8 additions and 11 deletions

View File

@@ -1,5 +1,4 @@
using System.IO;
using System.Reflection;
using System.Text;
namespace System.Security.Cryptography
@@ -19,16 +18,14 @@ namespace System.Security.Cryptography
/// <param name="keyFile">The (absolute) path to the crypto key file. On <c>null</c> the file 'crypto.key' at the executing assembly location will be used.</param>
public CryptographyHelper(string keyFile = null)
{
if (string.IsNullOrWhiteSpace(keyFile))
keyFile = "crypto.key";
if (!Path.IsPathRooted(keyFile))
{
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
keyFile = Path.Combine(dir, keyFile);
}
masterKeyFile = keyFile;
if (string.IsNullOrWhiteSpace(masterKeyFile))
masterKeyFile = "crypto.key";
if (!Path.IsPathRooted(masterKeyFile))
masterKeyFile = Path.Combine(AppContext.BaseDirectory, masterKeyFile);
string pw = File.Exists(masterKeyFile) ? File.ReadAllText(masterKeyFile) : null;
if (string.IsNullOrWhiteSpace(pw))
File.WriteAllText(masterKeyFile, GetRandomString(64));