d02540311 2017-04-09 17:37 采纳率: 0%
浏览 912

c#操作文件写入与打开的解决方法

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(&quot; + this.ListBox1.SelectedItem.ToString() + &quot;); ");
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-10 02:04
    关注

    写了一堆代码,你的问题是什么,如果代码根本不是你写的,谁写的问谁去。这代码太乱了

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?