-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (42 loc) · 1.96 KB
/
Program.cs
File metadata and controls
50 lines (42 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Diagnostics;
namespace EncryptionConfig
{
internal class Program
{
static void Main(string[] args)
{
EncryptString(true);
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["ConnectionString01"];
Console.WriteLine(connectionString);
}
public static void EncryptString(bool encrypt)
{
Configuration? config = null;
try
{
// open app.config and retrieve connectionStrings-section
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection connectionStringsSection = config.GetSection("connectionStrings") as ConnectionStringsSection;
// if - section & element are not locked
if ((!connectionStringsSection.ElementInformation.IsLocked) && (!connectionStringsSection.SectionInformation.IsLocked))
{
// if - enrypt the section
if (encrypt && !connectionStringsSection.SectionInformation.IsProtected)
{
connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
// if - enrypt is false --> decrypt
if (!encrypt && connectionStringsSection.SectionInformation.IsProtected)
{
connectionStringsSection.SectionInformation.UnprotectSection();
}
// save the new app.config and open in notepad
connectionStringsSection.SectionInformation.ForceSave = true;
config.Save();
Process.Start("notepad.exe", config.FilePath);
}
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
}
}