App.xml
<?xml version="1.0" encoding="utf-8" ?>
<app>
<add key = "1" value="C:/WIndows/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis.exe"/>
<add key = "2" value="MyKeys"/>
<add key = "3" value="/Demo"/><!--虛擬目錄路徑-->
<add key = "4" value="ecnc0048/ASPNET"/> <!--ASPNET帳號-->
</app>
C# Code:
static void Main(string[] args)
{
Console.Title = "WebConfig ConnectionStrings Tool For .NET2.0";
Console.WriteLine("WebConfig ConnectionStrings Encryption and Declassified Tool/n");
Console.WriteLine("1)Encryption WebConfig ConnectionStrings.");
Console.WriteLine("2)Declassified WebConfig ConnectionStrings.");
Console.WriteLine("3)Exit WebConfig./n");
Console.Write("Please select number: ");
string strIn = Console.ReadLine().Replace("Please select number:", "");
switch (strIn)
{
case "1"://加密
RunCmd("1");
break;
case "2"://解密
RunCmd("2");
break;
case "3"://退出
break;
default:
Console.WriteLine("/n Input error./n");
Main(args);
break;
}
//Console.WriteLine(RunCmd(@"C:/WIndows/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis -pc MyKeys -exp"));
//Console.WriteLine(RunCmd(@"C:/WIndows/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis -pe connectionStrings -app /Demo"));
//Console.WriteLine(RunCmd(@"C:/WIndows/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis -pa NetFrameworkConfigurationKey ecnc0048/ASPNET"));
//Console.WriteLine(RunCmd(@"C:/WIndows/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis -pa MyKeys ecnc0048/ASPNET"));
}
private static void RunCmd(string select)
{
string xmlFile = System.IO.Path.GetFullPath(@"App.xml");
if (File.Exists(xmlFile))
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlFile);
XmlNode xNode;
XmlElement xElem1, xElem2, xElem3, xElem4;
xNode = xDoc.SelectSingleNode("//app");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='1']");
xElem2 = (XmlElement)xNode.SelectSingleNode("//add[@key='2']");
xElem3 = (XmlElement)xNode.SelectSingleNode("//add[@key='3']");
xElem4 = (XmlElement)xNode.SelectSingleNode("//add[@key='4']");
if (xElem1 != null)
{
string command = xElem1.GetAttribute("value");
string cmdKey = xElem2.GetAttribute("value");
string iisPath = xElem3.GetAttribute("value");
string iisUser = xElem4.GetAttribute("value");
if (File.Exists(command))
{
if (select == "1")
{
cmd(command + " -pc " + cmdKey + " -exp");
cmd(command + " -pe connectionStrings -app " + iisPath);
cmd(command + " -pa NetFrameworkConfigurationKey " + iisUser);
cmd(command + " -pa " + cmdKey + " " + iisUser);
}
else if (select == "2")
{
cmd(command + " -pd connectionStrings -app " + iisPath);
}
Console.WriteLine("Please Enter Exit.");
Console.ReadLine();
}
else
{
Console.WriteLine(command + " file does not exist.");
Console.WriteLine("Please check App.xml file./n");
Main(null);
return;
}
}
}
else
{
Console.WriteLine("App.XML file does not exist./n");
Main(null);
return;
}
}
private static void cmd(string command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //指定程序名
p.StartInfo.Arguments = "/c " + command; //定程式
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向入
p.StartInfo.RedirectStandardOutput = true; //重定向出
p.StartInfo.RedirectStandardError = true; //重定向出
p.StartInfo.CreateNoWindow = true; //置不示窗口
p.Start(); //€€€€
//p.StandardInput.WriteLine(command);
//p.StandardInput.WriteLine("exit");
//return p.StandardOutput.ReadToEnd();
Console.WriteLine(p.StandardOutput.ReadToEnd());
}