读写文件操作
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> 修改配置文件后台 </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
</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" /> 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

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


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

{16
if (!IsPostBack)17

{18
19
}20
}21

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

{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)31

{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) 41

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

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

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

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

{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


浙公网安备 33010602011771号