在asp.net中使用streamwriter的困难 [英] difficulty in work with streamwriter in asp.net

查看:50
本文介绍了在asp.net中使用streamwriter的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么文件没有创建?

这是我的代码

hi
why the file dont created?
it is my code

private void chng_OnClick(object sender, EventArgs e)
   {

       string pass = txt_pass.Text;
       string passhash = MD5Hash(pass);
       string path = Server.MapPath(".");
       StreamWriter file = new StreamWriter(path+"log.txt");

       file.WriteLine(passhash);
       file.Close();

       txt_pass.Text = "";
   }
   public static string MD5Hash(string text)
   {
       MD5 md5 = new MD5CryptoServiceProvider();

       //compute hash from the bytes of text
       md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

       //get hash result after compute it
       byte[] result = md5.Hash;

       StringBuilder strBuilder = new StringBuilder();
       for (int i = 0; i < result.Length; i++)
       {
           //change it into 2 hexadecimal digits
           //for each byte
           strBuilder.Append(result[i].ToString("x2"));
       }

       return strBuilder.ToString();
   }

推荐答案

一对建议..



1.创建新的streamwriter实例时,可以指定文件访问模式。例如,打开或创建,追加。



2.在file.close()方法之前,刷新缓冲区即file.flush()。 Flush将清除缓冲区并将流写入文件。
Couple of suggestions..

1. When you create new instance of streamwriter, you can specify the file access mode. For example, open or create, append.

2. Before the file.close() method, flush the buffer i.e file.flush(). Flush will clear the buffer and write the stream to file.


这篇关于在asp.net中使用streamwriter的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆