读写文件操作

 前台页代码如下:
前台页代码如下:1
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlupdate.aspx.cs" Inherits="admin_xmlupdate" ValidateRequest="false" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlupdate.aspx.cs" Inherits="admin_xmlupdate" ValidateRequest="false" %>2

3
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
 <html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">6
 <head runat="server">
<head runat="server">7
 <link href="static/main.css" rel="stylesheet" type="text/css" />
<link href="static/main.css" rel="stylesheet" type="text/css" />8
 <title>修改配置文件后台</title>
    <title>修改配置文件后台</title>9
 </head>
</head>10
 <body>
<body>11
 <form id="form1" runat="server">
    <form id="form1" runat="server">12
 <div id="maindiv">
 <div id="maindiv">13
 <fieldset>
<fieldset>14
 <legend><b> 修改配置文件后台  </b></legend>
<legend><b> 修改配置文件后台  </b></legend>15
 <table cellpadding="0" cellspacing="0" border="0">
<table cellpadding="0" cellspacing="0" border="0">16
 <tr><td align="right"><strong>请选择你要修改的配置文件:</strong></td><td>
<tr><td align="right"><strong>请选择你要修改的配置文件:</strong></td><td>17
 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 18
 onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">19
 <asp:ListItem Value="-1">请选择</asp:ListItem>
        <asp:ListItem Value="-1">请选择</asp:ListItem>20
 <asp:ListItem Value="Notice.config">注会,中级最新公告</asp:ListItem>
        <asp:ListItem Value="Notice.config">注会,中级最新公告</asp:ListItem>21
 </asp:DropDownList>
    </asp:DropDownList>22
  </td></tr>
     </td></tr>23
 <tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>24
 <tr><td colspan="2">
<tr><td colspan="2">25
 <asp:TextBox ID="txtXML" runat="server" Height="800px" TextMode="MultiLine"
    <asp:TextBox ID="txtXML" runat="server" Height="800px" TextMode="MultiLine" 26
 Width="1200px"></asp:TextBox>
        Width="1200px"></asp:TextBox>27
 </td></tr>
    </td></tr>28
 <tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>29
 </table>
</table>30
 </fieldset>
</fieldset>31
 <br />
<br />32
 <div align="center">
<div align="center">33
 <asp:Button ID="Button1" runat="server" Text="修 改" CssClass="SubmitButton"
<asp:Button ID="Button1" runat="server" Text="修 改" CssClass="SubmitButton" 34
 onclick="Button1_Click" />     
        onclick="Button1_Click" />     35
 <asp:Button ID="Button3" runat="server" CssClass="SubmitButton"
    <asp:Button ID="Button3" runat="server" CssClass="SubmitButton" 36
 Text="取 消" />
        Text="取 消" />37
 </div>
</div>38
 <br />
<br />39

40

41
 </div>
</div>42
 </form>
    </form>43
 </body>
</body>44
 </html>
</html>45

 

 后台代码如下:
后台代码如下:1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Web;
using System.Web;4
 using System.Web.UI;
using System.Web.UI;5
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;6
 using PB.Business;
using PB.Business;7
 using PB.Common;
using PB.Common;8
 using PB.Data;
using PB.Data;9
 using PB.Entity;
using PB.Entity;10
 using System.IO;
using System.IO;11
 using WebMessageBox;
using WebMessageBox;12
 public partial class admin_xmlupdate : System.Web.UI.Page
public partial class admin_xmlupdate : System.Web.UI.Page13


 {
{14
 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)15

 
     {
{16
 if (!IsPostBack)
        if (!IsPostBack)17

 
         {
{18
 
           19
 }
        }20
 }
    }21

 /**//// <summary>
    /**//// <summary>22
 /// 获取所有的XML
    /// 获取所有的XML23
 /// </summary>
    /// </summary>24
 /// <returns></returns>
    /// <returns></returns>25
 public List<CategoryInfo> GetFileXml()
    public List<CategoryInfo> GetFileXml()26

 
     {
{27
 List<CategoryInfo> list=new List<CategoryInfo>();
        List<CategoryInfo> list=new List<CategoryInfo>();28
 DirectoryInfo di = new DirectoryInfo("F:\\vss-database\\PB.Web.root\\PB.Web\\PB.Web\\config");
        DirectoryInfo di = new DirectoryInfo("F:\\vss-database\\PB.Web.root\\PB.Web\\PB.Web\\config");29
 FileInfo[] fi = di.GetFiles("*.config");
        FileInfo[] fi = di.GetFiles("*.config");30
 foreach (FileInfo fiTemp in fi)
        foreach (FileInfo fiTemp in fi)31

 
         {
{32
 CategoryInfo info = new CategoryInfo();
            CategoryInfo info = new CategoryInfo();33
 info.Des = fiTemp.ToString();
            info.Des = fiTemp.ToString();34
 info.Value = fiTemp.ToString();
            info.Value = fiTemp.ToString();35
 list.Add(info);
            list.Add(info);36
 }
        }37
 return list;
        return list;38
 }
    }39

40
 public string ReadXml(string filename)
    public string ReadXml(string filename) 41

 
     {
{42
 String input;
        String input;43
 if (!File.Exists(filename))
        if (!File.Exists(filename))44

 
         {
{45
 MessageBox.Show("文件不存在", "/admin/xmlupdate.aspx");
            MessageBox.Show("文件不存在", "/admin/xmlupdate.aspx");46
 return "";
            return "";47
 }
        }48
 using (StreamReader sr = File.OpenText(filename))
        using (StreamReader sr = File.OpenText(filename))49

 
         {
{50
 input = sr.ReadToEnd();//读XML
            input = sr.ReadToEnd();//读XML51
 sr.Close();
            sr.Close();52
 }
        }53
 return input;
        return input;54
 }
    }55
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)56

 
     {
{57
 string path = Server.MapPath("/config/") + DropDownList1.SelectedValue;
        string path = Server.MapPath("/config/") + DropDownList1.SelectedValue;58
 txtXML.Text = ReadXml(path);
        txtXML.Text = ReadXml(path);59
 }
    }60
 protected void Button1_Click(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)61

 
     {
{62
 string path = Server.MapPath("/config/") + DropDownList1.SelectedValue;
        string path = Server.MapPath("/config/") + DropDownList1.SelectedValue;63
 File.SetAttributes(path,FileAttributes.Normal);//设置你想修改文件的读写权限为正常的
        File.SetAttributes(path,FileAttributes.Normal);//设置你想修改文件的读写权限为正常的64

65
 FileStream fs = new FileStream(path,FileMode.Create,FileAccess.ReadWrite);
        FileStream fs = new FileStream(path,FileMode.Create,FileAccess.ReadWrite);66
 StreamWriter sw = new StreamWriter(fs);
        StreamWriter sw = new StreamWriter(fs);67
 sw.Write(txtXML.Text);
        sw.Write(txtXML.Text);68
 sw.Flush();
        sw.Flush();69
 sw.Close();
        sw.Close();70
 fs.Close();
        fs.Close();71
 //File.WriteAllText(path, txtXML.Text);
        //File.WriteAllText(path, txtXML.Text);72
 //File.Copy(path, txtXML.Text);
        //File.Copy(path, txtXML.Text);73
 MessageBox.Show("修改成功!", "/admin/xmlupdate.aspx");
        MessageBox.Show("修改成功!", "/admin/xmlupdate.aspx");74
 }
    }75
 }
}76

 
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号