读写文件操作

 

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

 


 

ContractedBlock.gifExpandedBlockStart.gif后台代码如下:
 1using System;
 2using System.Collections.Generic;
 3using System.Web;
 4using System.Web.UI;
 5using System.Web.UI.WebControls;
 6using PB.Business;
 7using PB.Common;
 8using PB.Data;
 9using PB.Entity;
10using System.IO;
11using WebMessageBox;
12public partial class admin_xmlupdate : System.Web.UI.Page
13ExpandedBlockStart.gifContractedBlock.gif{
14    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    {
16        if (!IsPostBack)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        {
18           
19        }

20    }

21ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
22    /// 获取所有的XML
23    /// </summary>
24    /// <returns></returns>

25    public List<CategoryInfo> GetFileXml()
26ExpandedSubBlockStart.gifContractedSubBlock.gif    {
27        List<CategoryInfo> list=new List<CategoryInfo>();
28        DirectoryInfo di = new DirectoryInfo("F:\\vss-database\\PB.Web.root\\PB.Web\\PB.Web\\config");
29        FileInfo[] fi = di.GetFiles("*.config");
30        foreach (FileInfo fiTemp in fi)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
32            CategoryInfo info = new CategoryInfo();
33            info.Des = fiTemp.ToString();
34            info.Value = fiTemp.ToString();
35            list.Add(info);
36        }

37        return list;
38    }

39
40    public string ReadXml(string filename) 
41ExpandedSubBlockStart.gifContractedSubBlock.gif    {
42        String input;
43        if (!File.Exists(filename))
44ExpandedSubBlockStart.gifContractedSubBlock.gif        {
45            MessageBox.Show("文件不存在""/admin/xmlupdate.aspx");
46            return "";
47        }

48        using (StreamReader sr = File.OpenText(filename))
49ExpandedSubBlockStart.gifContractedSubBlock.gif        {
50            input = sr.ReadToEnd();//读XML
51            sr.Close();
52        }

53        return input;
54    }

55    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
56ExpandedSubBlockStart.gifContractedSubBlock.gif    {
57        string path = Server.MapPath("/config/"+ DropDownList1.SelectedValue;
58        txtXML.Text = ReadXml(path);
59    }

60    protected void Button1_Click(object sender, EventArgs e)
61ExpandedSubBlockStart.gifContractedSubBlock.gif    {
62        string path = Server.MapPath("/config/"+ DropDownList1.SelectedValue;
63        File.SetAttributes(path,FileAttributes.Normal);//设置你想修改文件的读写权限为正常的
64
65        FileStream fs = new FileStream(path,FileMode.Create,FileAccess.ReadWrite);
66        StreamWriter sw = new StreamWriter(fs);
67        sw.Write(txtXML.Text);
68        sw.Flush();
69        sw.Close();
70        fs.Close();
71        //File.WriteAllText(path, txtXML.Text);
72        //File.Copy(path, txtXML.Text);
73        MessageBox.Show("修改成功!""/admin/xmlupdate.aspx");
74    }

75}

76


 

转载于:https://www.cnblogs.com/epwqgdnvrhok/archive/2009/01/06/1370110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值