public partial class read_write_file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// String ss = " Log CreateDate : 2017年4月9日 0:39:38";//35
//String ss = "-";//1
// string aa = "abcdef";
// int a = aa.indexOf("bc");//a会等于1
string ss = GeneralString(100, "-");
Response.Write(ss.LastIndexOf("-"));
// Response.Write(GeneralString(35,"-"));
}
private static string GeneralString(int n, string par)
{
System.Text.StringBuilder a = new System.Text.StringBuilder();
for (int i = 0; i < n; i++)
{
a.Append(par);
}
return a.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
//Response.Write("<script type=\"text/JavaScript\">alert(" + this.ListBox1.SelectedItem.ToString() + ");</script> ");
int index = ListBox1.SelectedIndex;
string ss = ListBox1.Items[index].Value;
// Response.Write("<script type=\"text/JavaScript\">alert('"+ss+ "');</script> ");
//bool ListBoxState = false;//未选中的时候该值为false
// foreach (ListItem item in this.ListBox1.Items)
{
// if (item.Selected)
// {
//ListBoxState = true;//有某一项被选中时该值就为true
// Response.Write(item.Text);
// }
// }
//if (ListBox1.SelectedIndex != -1)
//{
// // MessageBox.Show(listBox1.SelectedItem.ToString());
// //MessageBox.Show(this.ListBox1.SelectedItem.ToString());
// // 原来MessageBox.Show(es.Message); 这句话在发布的服务器上无法执行,替换成如下语句
// Response.Write("<script type=\"text/JavaScript\">alert(" + this.ListBox1.SelectedItem.ToString() + ");</script> ");
//}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
String file_path = TextBox1.Text;
String str_wirte = TextBox2.Text.Trim();
if (!File.Exists(file_path))///不存在创建
{
Response.Write("<font color='red'>" + file_path + "不存在,请检查!</font>");
}
else if (str_wirte=="" || str_wirte ==null) {//内容不能为空
Response.Write("<font color='red'>输入内容不能为空,请检查!</font>");
}
else
{
using (StreamWriter w = File.AppendText(file_path))
{
Log(str_wirte, w);
}
Button3_Click( sender, e);
};
}
public void Log(String logMessage, TextWriter w)
{//写入函数
w.Write("\r\n Log CreateDate : ");
w.Write("{0} {1}",DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
w.Write("\0 ==>");
w.WriteLine(" {0}", logMessage);
w.WriteLine(GeneralString(100,"_"));
}
public void DumpLog(StreamReader r)
{ //循环输出到lisitbox
// While not at the end of the file, read and write lines.
String line;
int i = 1;
while ((line = r.ReadLine()) != null)
{
//Console.WriteLine(line);
if (line != "" && line.LastIndexOf("_")!= 99)
{
ListBox1.Items.Add(i + line);
i++;
}
else if(line.LastIndexOf("_") == 99)
{
ListBox1.Items.Add(line);
}
}
r.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
String file_path = TextBox1.Text;
ListBox1.Items.Clear();
if (!File.Exists(file_path))///不存在创建
{
Response.Write("<font color='red'>" + file_path + "不存在,请检查!</font>");
}
else {
using (StreamReader r = File.OpenText(file_path))
{
DumpLog(r);
}
}
}
protected void Button4_Click(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)//删除之前记得判断是否有选中项
{
//获取当前选项的索引号
int index = ListBox1.SelectedIndex;
//ListBox1.ClearSelection();
ListItem item = ListBox1.Items[index];
ListBox1.Items.Remove(item);
}
}
protected void Button6_Click(object sender, EventArgs e)
{
}
protected void Button5_Click(object sender, EventArgs e)
{
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ListBox1_TextChanged(object sender, EventArgs e)
{
//ListBox1.SelectedItem.Text =文本
//ListBox1.SelectedItem.Value 值
TextBox3.Text = "当前选择列表索引值:" + ListBox1.SelectedItem.Text;
}
//private void listBox1_DoubleClick(object sender, EventArgs e)
//{
// MessageBox.Show(this.ListBox1.SelectedItem.ToString());
//}
protected void listBox1_MouseClick(object sender, EventArgs e)
{
// int index = ListBox1.ine.X, e.Y);
//int index = ListBox1.SelectedIndex;
//Response.Write("<script type=\"text/JavaScript\">alert(" + this.ListBox1.SelectedItem.ToString() + ");</script> ");
// if (ListBox1.SelectedIndex != -1)
// {
// // MessageBox.Show(listBox1.SelectedItem.ToString());
// //MessageBox.Show(this.ListBox1.SelectedItem.ToString());
// // 原来MessageBox.Show(es.Message); 这句话在发布的服务器上无法执行,替换成如下语句
//Response.Write("alert(" + this.ListBox1.SelectedItem.ToString() + "); ");
}
}