12-08整理

①
//cs页中如何指定编码方式
UnicodeEncoding.GetEncoding("gb2312") 或者 Encoding.UTF8

②
//.net文件读写
 public string FileName //使用属性存储文件路径
{
    get { return this.Server.MapPath("asd.txt"); }
}
#region 文件操作
    // 创建txt文件
    public void CreateToFile()
    {
        StreamWriter SW;
        SW = File.CreateText(FileName);
        SW.Close();
    }
    // 写文件
    public void WriteToFile()
    {
        string InsertStr = "";
        if (!File.Exists(FileName))
        {
            CreateToFile();
            InsertStr = "Content#DateTime#!";    //txt文件中用它来表示字段
        }
        InsertStr +=TextBox1.Text.ToString() + "#";
        InsertStr += Convert.ToString(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd")) + "#";
        File.AppendAllText(FileName, InsertStr + "!", Encoding.UTF8);
    }
    // 读取文件
    public string ReadTextFileDate()
    {
        string strInput = "";
        string GetStream = "";
        if (File.Exists(FileName))
        {
            StreamReader sr = new StreamReader(FileName, UnicodeEncoding.UTF8);
            strInput = sr.ReadLine();//UnicodeEncoding.GetEncoding("gb2312")
            while (strInput != null)
            {
                GetStream += strInput;
                strInput = sr.ReadLine();
            }
            sr.Close();
        }
        else
        {
            GetStream = "此文本不存在";
            CreateToFile();//创建此文本
        }
        return GetStream;
    }    
    // 修改txt中的内容
    public void UpdateTextFile(string FileName, string StrCount)
    {
        System.IO.StreamReader sr = new System.IO.StreamReader(FileName);
        string s = sr.ReadToEnd();
        sr.Close();
        s = s.Replace(s, StrCount);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName, false);//,Encoding.UTF8
        sw.Write(s);
        sw.Close();
        Response.Redirect(Request.Url.ToString());
    }
	// 删除文件
	 public string DeleteFile(string FileName)
    {
        string title=string.Empty;
        try
        {//删除文件
            File.Delete(FileName);
            Label lblOk = new Label();
            title = "删除文件" + FileName + "成功";
        }
        catch (Exception ee)
        { //捕捉异常
            Label lblFileExists = new Label();
            title = "不能删除文件" + FileName + "<br>";
        }
        return title;
    }
	//复制文件
    //Copy方法还可以带1个bool类型的可选参数,如果将其值设置为true,表示如果目标文件已经存在,则不提醒用户就覆盖原来的文件。
    public void copyFile(string OrignFile, string NewFile)
    {
        //首先判断源文件是否存在
        if (File.Exists(OrignFile))
        {
            //拷贝文件
            try { File.Copy(OrignFile, NewFile); }
            catch (Exception ee)
            {
              HttpContext.Current.Response.Write("不能拷贝文件");
            }
        }
        else HttpContext.Current.Response.Write(OrignFile + "不存在<br>");
    }
#endregion

//获得文件的创建时间
FileInfo fi = new FileInfo(FileName);
DateTime CreateTime = fi.CreationTime;
TextBox1.Text += "创建时间为:" + CreateTime.ToString();

③
//StringBuilder的Append方法
StringBuilder类是String类的继承类,StringBuilder类拥有更多的处理字符串的方法,
Append是把多个字符串结成一个整体,但在最后显示其内容时,必须要用ToString()方法把StringBuilder类的对象实例转换成string类型。
例:
StringBuilder output = new StringBuilder();
for(int i=0;i<10;i++)
output.Append(i.ToString());
Response.Write(output.ToString());

④
FileStream 类中的权限共有三种
1,FileAccess.Write, //写
2,FileAccess.ReadWrite, //读写
3,FileAccess.Read //读

⑤
javascript的 defer属性强制页面加载完成后来再运行js
HTML5中javascript有async函数,作用:并行加载,并在可用时立即执行,它的值是一个bool,即true或false。但必须有src属性

js在页面载入时的执行顺序
1. 【head部】脚本(无defer属性) 
2. 【.net后台】注册的Block脚本 
3. 【body部】脚本(无defer属性) 
4. 【.net后台】注册的StartUp脚本 
5. 含defer属性的脚本 
6. window.onload 
7. jQuery的加载函数(无defer属性) 
8. jQuery的加载函数(含defer属性)


⑤
random.Next(240) //生成一个小于指定数字的非负随机数.

⑥
//手动生成DataTable
 private string firstName = "赵钱孙李周吴郑王冯陈诸卫蒋沈韩杨朱秦尤许何吕施张孔曹严华";
 private string lastName = "猛勇刚强豹彪雁燕蓉菲";
//手动生成DataTable
private DataTable CreateDataTable()
{
        DataTable data = new DataTable();
        DataColumn dcId = new DataColumn("ID", typeof(Int32));
        //设置ID列自动递增
        dcId.AutoIncrement = true;
        //设置ID列初始值为1
        dcId.AutoIncrementSeed = 1;
        //设置ID列递增步长为1
        dcId.AutoIncrementStep = 1;
        //将ID列添加到DataTable中
        data.Columns.Add(dcId);
        data.Columns.Add(new DataColumn("Name", typeof(string)));
        data.Columns.Add(new DataColumn("Age", typeof(int)));
        data.Columns.Add(new DataColumn("Sex", typeof(bool)));
        data.Columns.Add(new DataColumn("Married", typeof(bool)));
        DataRow dataRow = null;
        Random random = new Random();
        //随机生成20条记录
        for (int i = 0; i < 20; i++)
        {
            dataRow = data.NewRow();
            //随机生成姓名
            dataRow["Name"] = firstName.Substring(random.Next(firstName.Length), 1) + lastName.Substring(random.Next(lastName.Length), 1);
            //随即生成介于20至100之间的年龄
            int age = random.Next(20, 100);
            dataRow["Age"] = age;
            //随即设置性别
            bool sex = (random.Next(100) % 2 == 0) ? true : false;
            dataRow["Sex"] = sex;
            if (((sex == true) && (age >= 22)) || ((sex == false) && (age >= 20)))//男性结婚年龄大于22周岁,女性结婚年龄大于20周岁
            {
                dataRow["Married"] = (random.Next(500) % 2 == 0) ? true : false;
            }
            else
            { dataRow["Married"] = false; }
            data.Rows.Add(dataRow);
        }
        return data;
}
posted @ 2010-12-08 17:49  kelin418  阅读(188)  评论(0编辑  收藏  举报