各位大侠好,小弟今天下午用C#写了半天代码了,目的是修改web.config的内容,结果是根本没有达到效果,大哥大姐们,给点建议吧!!
我的web.config如下:
<?xml version="1.0"?>
<configuration>
  <appSettings>
     <add   key="Ctrl_connection"   value="Access"  />
     <add  key="Ctrl_node" value="have_injection"/>
  </appSettings>
  <connectionStrings/>
<system.web>
<compilation debug="true">
    <assemblies>
<add assembly="Accessibility, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.VisualC, Version=8.0.0.0,Culture=neutral,PublicKeyToken=B03F5F7F11D50A3A"/>
    </assemblies>
</compilation>

<authentication mode="Windows"/>
             <customErrors mode="off" defaultRedirect="GenericErrorPage.htm">
             <error statusCode="403" redirect="NoAccess.htm" />
             <error statusCode="404" redirect="FileNotFound.htm" />
             </customErrors>
        
</system.web>
</configuration>
就是想修改appSettings段的内容,我的C#代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;namespace WindowsApplication_for_injection
{
    
   
    public partial class Form1 : Form
    {
        [DllImport("kernel32")]
        private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, char[] retVal, int siae, string filePath);           public Form1()
        {
            InitializeComponent();         }        private void button1_Click(object sender, EventArgs e)
        {
       
            WritePrivateProfileString("appSettings", "Ctrl_DB", "Access", @"F:\sql注入实验项目\asp源代码\新闻发布系统\web.config");
            label2.Text = "Access数据库";
        }        private void button2_Click(object sender, EventArgs e)
        {
            WritePrivateProfileString("appSettings", "Ctrl_node", "SQLserver", @"F:\sql注入实验项目\asp源代码\新闻发布系统\web.config");
            label2.Text = "SQLserver数据库";        }        private void button5_Click(object sender, EventArgs e)
        {
            char [] str=new char[256];
            int i = GetPrivateProfileString("appSettings", "Ctrl_node", "应用程序出错", str, 255, @"F:\sql注入实验项目\asp源代码\新闻发布系统\web.config");   
            if (str.ToString() == "have_injection")
            {
                int j=WritePrivateProfileString("appSettings", "Ctrl_node", "no_injection", @"F:\sql注入实验项目\asp源代码\新闻发布系统\web.config");
                label3.Text = "无漏洞代码";
            }
            else if (str.ToString() == "no_injection")
            {
                int k=WritePrivateProfileString("appSettings", "Ctrl_node", "have_injection", @"F:\sql注入实验项目\asp源代码\新闻发布系统\web.config");
                label2.Text = "有漏洞代码";
            }
        }    }
}
就是想通过单击button,对web.config进行更改,可是达不到效果啊!!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    namespace XSCUTIL.UTIL
    {
        /// <summary>
        /// 修改配置文件类
        /// </summary>
        public class CConfigXml
        {
            //操作的配置文档
            private XmlDocument xd = null;
            //定义的异常类
            //private CException ce = new CException();        #region 调用方法
            /*
            LoadFile("");
            SetAppSettings("","");
            SaveFile("","");
             */
            #endregion        #region 加载配置文件
           /// <summary>
            /// 加载配置文件
           /// </summary>
           /// <param name="strStartPath">配置文件路径</param>
           /// <returns></returns>
            public bool  LoadFile(String strStartPath)
            {
                //此处配置文件在程序目录下
          
                try
                {
                    xd = new XmlDocument();
                    xd.Load(strStartPath);
                    return true; 
                }
                catch (Exception e)
                {
                    //ce.SaveExcetpion(e);
                    return false;
                }        }
            #endregion                      #region 修改appSettings下的值
            /// <summary>
            /// 键名 
            /// </summary>
            /// <param name="key"></param>
            /// <param name="?"></param>
            /// <returns></returns>
           public bool SetAppSettings(String key,String value)
           {
               try
               {
                   XmlElement xe = xd.DocumentElement.SelectSingleNode("//appSettings/add[@key='"+key+"']") as XmlElement;
                   if (xe != null)
                   {
                       //xe.InnerText = value;
                       xe.SetAttribute("value", value);
                   }
                   return true; 
               }
               catch (Exception e)
               {
                   //ce.SaveExcetpion(e);
                   return false;
               }
               
           }
            #endregion        #region 保存配置文件       public bool SaveFile(String strStartPath)
            {
                try
                {
                  
                    if (xd != null)
                    {
                        xd.Save(strStartPath);
                    }
                    return true; 
                }
                catch (Exception e)
                {
                    //ce.SaveExcetpion(e);
                    return false;
                }
            }
            #endregion       
        }
    } 我这一个简单的 你看看