问题
I have a class within a dll that has the following methods for writing to the registry:
public Boolean WriteDBConnectionInfoToRegistry(string txtDSN, string txtServer, string txtDatabase, string txtUser, string txtPassword, bool trusted)
{
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
rijn.GenerateKey();
rijn.GenerateIV();
Boolean b1 = RegistryWrite("Software\\MyApp", "IV", Reverse(Convert.ToBase64String(rijn.IV)));
Boolean b2 = RegistryWrite("Software\\MyApp", "Key", Reverse(Convert.ToBase64String(rijn.Key)));
Boolean b3 = RegistryWrite("Software\\MyApp", "DSN", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtDSN))));
Boolean b4 = RegistryWrite("Software\\MyApp", "Server", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtServer))));
Boolean b5 = RegistryWrite("Software\\MyApp", "Database", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtDatabase))));
Boolean b6 = RegistryWrite("Software\\MyApp", "User", EncryptString(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtUser))),rijn.IV,rijn.Key));
Boolean b7 = RegistryWrite("Software\\MyApp", "Password", EncryptString(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(txtPassword))), rijn.IV, rijn.Key));
Boolean b8 = RegistryWrite("Software\\MyApp", "Trusted", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Reverse(trusted ? "1" : "0"))));
if (b1 && b2 && b3 && b4 & b5 & b6 & b7 & b8)
{
return true;
}
else
{
return false;
}
}
private bool RegistryWrite(string subKey, string KeyName, object Value)
{
try
{
RegistryKey rk = Registry.LocalMachine;
RegistryKey sk1 = rk.CreateSubKey(subKey);
sk1.SetValue(KeyName.ToUpper(), Value);
return true;
}
catch (Exception e)
{
}
return false;
}
The methods are contained within a public class, that is part of a dll that I have installed in a ASP.NET web page. The methods successfully write to the registry when called from a Windows application to test, however when the method is called from the code behind in the web site, it fails to write to create the folder and keys, despite returning true. How can I fix this? The framework for the .dll is .Net 2.0 and the web application is in 4.0. Thank you for the help.
来源:https://stackoverflow.com/questions/13631235/c-sharp-dll-method-call-fails-to-write-and-read-to-from-the-registry-when-called