app.configファイル内のカスタムセクションを使用できます(適切な代替手段)。データ(キー)を入力してから、構成ファイルを暗号化します。サンプルは次のとおりです。
//loading config file...
config = ConfigurationManager.OpenExeConfiguration("AvtoNetPublisher.exe");
//getting specific section (in this case custom class)
csp = (GmailSettingsProvider)config.GetSection("gsp");
if (!csp.SectionInformation.IsProtected)
{
DialogResult result = MessageBox.Show("Configuration is not protected, proceed to enter configuration!", "Configuration is not protected", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result != DialogResult.OK)
{
throw new Exception("Configuration was interupted by user!");
}
csp.UserName = Microsoft.VisualBasic.Interaction.InputBox("Enter user name", "UserName", csp.UserName, Cursor.Position.X, Cursor.Position.Y);
csp.Password = Microsoft.VisualBasic.Interaction.InputBox("Enter password", "Password", csp.Password, Cursor.Position.X, Cursor.Position.Y);
csp.ImapServer = Microsoft.VisualBasic.Interaction.InputBox("Enter ImapServer", "ImapServer", csp.ImapServer, Cursor.Position.X, Cursor.Position.Y);
csp.Port = Convert.ToInt16(Microsoft.VisualBasic.Interaction.InputBox("Enter int32 Port", "Port", csp.Port.ToString(), Cursor.Position.X, Cursor.Position.Y));
csp.EnableSSL = Convert.ToBoolean(Microsoft.VisualBasic.Interaction.InputBox("Enter bool EnableSSL", "EnableSSL", csp.EnableSSL.ToString(), Cursor.Position.X, Cursor.Position.Y));
csp.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
これは、アプリの最初のインストールまたはロード時にトリガーできます。