WebConfig ConnectionStrings Tool For .NET2.0

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());
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值